blob: 92f74aafb05c5688d191cd4964b4ad64c22ebed5 [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);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500462
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500463 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200464 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400465
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200466 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
467 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200468 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
469 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500470
471 weston_surface_state_init(&surface->pending);
472
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400473 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500474 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300475 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400476
Jason Ekstranda7af7042013-10-12 22:38:11 -0500477 wl_list_init(&surface->views);
478
479 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400480 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500481
Pekka Paalanene67858b2013-04-25 13:57:42 +0300482 wl_list_init(&surface->subsurface_list);
483 wl_list_init(&surface->subsurface_list_pending);
484
Jason Ekstrand1e059042014-10-16 10:55:19 -0500485 weston_matrix_init(&surface->buffer_to_surface_matrix);
486 weston_matrix_init(&surface->surface_to_buffer_matrix);
487
Jonas Ådahld3414f22016-07-22 17:56:31 +0800488 wl_list_init(&surface->pointer_constraints);
489
Bryce Harringtonf8300c82016-08-10 17:25:22 -0700490 surface->inhibit_idling = false;
491
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400492 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500493}
494
Alex Wu8811bf92012-02-28 18:07:54 +0800495WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500496weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200497 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500498{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100499 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500500}
501
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400502WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500503weston_view_to_global_float(struct weston_view *view,
504 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200505{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500506 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200507 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
508
Jason Ekstranda7af7042013-10-12 22:38:11 -0500509 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200510
511 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200512 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700513 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200514 v.f[3]);
515 *x = 0;
516 *y = 0;
517 return;
518 }
519
520 *x = v.f[0] / v.f[3];
521 *y = v.f[1] / v.f[3];
522 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500523 *x = sx + view->geometry.x;
524 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200525 }
526}
527
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500528WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200529weston_transformed_coord(int width, int height,
530 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200531 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200532 float sx, float sy, float *bx, float *by)
533{
534 switch (transform) {
535 case WL_OUTPUT_TRANSFORM_NORMAL:
536 default:
537 *bx = sx;
538 *by = sy;
539 break;
540 case WL_OUTPUT_TRANSFORM_FLIPPED:
541 *bx = width - sx;
542 *by = sy;
543 break;
544 case WL_OUTPUT_TRANSFORM_90:
545 *bx = height - sy;
546 *by = sx;
547 break;
548 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
549 *bx = height - sy;
550 *by = width - sx;
551 break;
552 case WL_OUTPUT_TRANSFORM_180:
553 *bx = width - sx;
554 *by = height - sy;
555 break;
556 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
557 *bx = sx;
558 *by = height - sy;
559 break;
560 case WL_OUTPUT_TRANSFORM_270:
561 *bx = sy;
562 *by = width - sx;
563 break;
564 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
565 *bx = sy;
566 *by = sx;
567 break;
568 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200569
570 *bx *= scale;
571 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200572}
573
574WL_EXPORT pixman_box32_t
575weston_transformed_rect(int width, int height,
576 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200577 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200578 pixman_box32_t rect)
579{
580 float x1, x2, y1, y2;
581
582 pixman_box32_t ret;
583
Alexander Larsson4ea95522013-05-22 14:41:37 +0200584 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200585 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200586 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200587 rect.x2, rect.y2, &x2, &y2);
588
589 if (x1 <= x2) {
590 ret.x1 = x1;
591 ret.x2 = x2;
592 } else {
593 ret.x1 = x2;
594 ret.x2 = x1;
595 }
596
597 if (y1 <= y2) {
598 ret.y1 = y1;
599 ret.y2 = y2;
600 } else {
601 ret.y1 = y2;
602 ret.y2 = y1;
603 }
604
605 return ret;
606}
607
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600608/** Transform a region by a matrix, restricted to axis-aligned transformations
609 *
610 * Warning: This function does not work for projective, affine, or matrices
611 * that encode arbitrary rotations. Only 90-degree step rotations are
612 * supported.
613 */
614WL_EXPORT void
615weston_matrix_transform_region(pixman_region32_t *dest,
616 struct weston_matrix *matrix,
617 pixman_region32_t *src)
618{
619 pixman_box32_t *src_rects, *dest_rects;
620 int nrects, i;
621
622 src_rects = pixman_region32_rectangles(src, &nrects);
623 dest_rects = malloc(nrects * sizeof(*dest_rects));
624 if (!dest_rects)
625 return;
626
627 for (i = 0; i < nrects; i++) {
628 struct weston_vector vec1 = {{
629 src_rects[i].x1, src_rects[i].y1, 0, 1
630 }};
631 weston_matrix_transform(matrix, &vec1);
632 vec1.f[0] /= vec1.f[3];
633 vec1.f[1] /= vec1.f[3];
634
635 struct weston_vector vec2 = {{
636 src_rects[i].x2, src_rects[i].y2, 0, 1
637 }};
638 weston_matrix_transform(matrix, &vec2);
639 vec2.f[0] /= vec2.f[3];
640 vec2.f[1] /= vec2.f[3];
641
642 if (vec1.f[0] < vec2.f[0]) {
643 dest_rects[i].x1 = floor(vec1.f[0]);
644 dest_rects[i].x2 = ceil(vec2.f[0]);
645 } else {
646 dest_rects[i].x1 = floor(vec2.f[0]);
647 dest_rects[i].x2 = ceil(vec1.f[0]);
648 }
649
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600650 if (vec1.f[1] < vec2.f[1]) {
651 dest_rects[i].y1 = floor(vec1.f[1]);
652 dest_rects[i].y2 = ceil(vec2.f[1]);
653 } else {
654 dest_rects[i].y1 = floor(vec2.f[1]);
655 dest_rects[i].y2 = ceil(vec1.f[1]);
656 }
657 }
658
659 pixman_region32_clear(dest);
660 pixman_region32_init_rects(dest, dest_rects, nrects);
661 free(dest_rects);
662}
663
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200664WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500665weston_transformed_region(int width, int height,
666 enum wl_output_transform transform,
667 int32_t scale,
668 pixman_region32_t *src, pixman_region32_t *dest)
669{
670 pixman_box32_t *src_rects, *dest_rects;
671 int nrects, i;
672
673 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
674 if (src != dest)
675 pixman_region32_copy(dest, src);
676 return;
677 }
678
679 src_rects = pixman_region32_rectangles(src, &nrects);
680 dest_rects = malloc(nrects * sizeof(*dest_rects));
681 if (!dest_rects)
682 return;
683
684 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
685 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
686 } else {
687 for (i = 0; i < nrects; i++) {
688 switch (transform) {
689 default:
690 case WL_OUTPUT_TRANSFORM_NORMAL:
691 dest_rects[i].x1 = src_rects[i].x1;
692 dest_rects[i].y1 = src_rects[i].y1;
693 dest_rects[i].x2 = src_rects[i].x2;
694 dest_rects[i].y2 = src_rects[i].y2;
695 break;
696 case WL_OUTPUT_TRANSFORM_90:
697 dest_rects[i].x1 = height - src_rects[i].y2;
698 dest_rects[i].y1 = src_rects[i].x1;
699 dest_rects[i].x2 = height - src_rects[i].y1;
700 dest_rects[i].y2 = src_rects[i].x2;
701 break;
702 case WL_OUTPUT_TRANSFORM_180:
703 dest_rects[i].x1 = width - src_rects[i].x2;
704 dest_rects[i].y1 = height - src_rects[i].y2;
705 dest_rects[i].x2 = width - src_rects[i].x1;
706 dest_rects[i].y2 = height - src_rects[i].y1;
707 break;
708 case WL_OUTPUT_TRANSFORM_270:
709 dest_rects[i].x1 = src_rects[i].y1;
710 dest_rects[i].y1 = width - src_rects[i].x2;
711 dest_rects[i].x2 = src_rects[i].y2;
712 dest_rects[i].y2 = width - src_rects[i].x1;
713 break;
714 case WL_OUTPUT_TRANSFORM_FLIPPED:
715 dest_rects[i].x1 = width - src_rects[i].x2;
716 dest_rects[i].y1 = src_rects[i].y1;
717 dest_rects[i].x2 = width - src_rects[i].x1;
718 dest_rects[i].y2 = src_rects[i].y2;
719 break;
720 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
721 dest_rects[i].x1 = height - src_rects[i].y2;
722 dest_rects[i].y1 = width - src_rects[i].x2;
723 dest_rects[i].x2 = height - src_rects[i].y1;
724 dest_rects[i].y2 = width - src_rects[i].x1;
725 break;
726 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
727 dest_rects[i].x1 = src_rects[i].x1;
728 dest_rects[i].y1 = height - src_rects[i].y2;
729 dest_rects[i].x2 = src_rects[i].x2;
730 dest_rects[i].y2 = height - src_rects[i].y1;
731 break;
732 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
733 dest_rects[i].x1 = src_rects[i].y1;
734 dest_rects[i].y1 = src_rects[i].x1;
735 dest_rects[i].x2 = src_rects[i].y2;
736 dest_rects[i].y2 = src_rects[i].x2;
737 break;
738 }
739 }
740 }
741
742 if (scale != 1) {
743 for (i = 0; i < nrects; i++) {
744 dest_rects[i].x1 *= scale;
745 dest_rects[i].x2 *= scale;
746 dest_rects[i].y1 *= scale;
747 dest_rects[i].y2 *= scale;
748 }
749 }
750
751 pixman_region32_clear(dest);
752 pixman_region32_init_rects(dest, dest_rects, nrects);
753 free(dest_rects);
754}
755
Jonny Lamb74130762013-11-26 18:19:46 +0100756static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300757viewport_surface_to_buffer(struct weston_surface *surface,
758 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100759{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200760 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200761 double src_width, src_height;
762 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200763
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200764 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
765 if (vp->surface.width == -1) {
766 *bx = sx;
767 *by = sy;
768 return;
769 }
Jonny Lamb74130762013-11-26 18:19:46 +0100770
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200771 src_x = 0.0;
772 src_y = 0.0;
773 src_width = surface->width_from_buffer;
774 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100775 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200776 src_x = wl_fixed_to_double(vp->buffer.src_x);
777 src_y = wl_fixed_to_double(vp->buffer.src_y);
778 src_width = wl_fixed_to_double(vp->buffer.src_width);
779 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100780 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200781
782 *bx = sx * src_width / surface->width + src_x;
783 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100784}
785
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500786WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200787weston_surface_to_buffer_float(struct weston_surface *surface,
788 float sx, float sy, float *bx, float *by)
789{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200790 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
791
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300792 /* first transform coordinates if the viewport is set */
793 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100794
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500795 weston_transformed_coord(surface->width_from_buffer,
796 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200797 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100798 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200799}
800
Derek Foreman99739672015-12-03 16:38:11 -0600801/** Transform a rectangle from surface coordinates to buffer coordinates
802 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300803 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600804 * from.
805 * \param rect The rectangle to transform.
806 * \return The transformed rectangle.
807 *
808 * Viewport and buffer transformations can only do translation, scaling,
809 * and rotations in 90-degree steps. Therefore the only loss in the
810 * conversion is coordinate rounding.
811 *
812 * However, some coordinate rounding takes place as an intermediate
813 * step before the buffer scale factor is applied, so the rectangle
814 * boundary may not be exactly as expected.
815 *
816 * This is OK for damage tracking since a little extra coverage is
817 * not a problem.
818 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200819WL_EXPORT pixman_box32_t
820weston_surface_to_buffer_rect(struct weston_surface *surface,
821 pixman_box32_t rect)
822{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200823 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100824 float xf, yf;
825
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300826 /* first transform box coordinates if the viewport is set */
827 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100828 rect.x1 = floorf(xf);
829 rect.y1 = floorf(yf);
830
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300831 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600832 rect.x2 = ceilf(xf);
833 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100834
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500835 return weston_transformed_rect(surface->width_from_buffer,
836 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200837 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200838 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200839}
840
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200841/** Transform a region from surface coordinates to buffer coordinates
842 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300843 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200844 * from.
845 * \param surface_region[in] The region in surface coordinates.
846 * \param buffer_region[out] The region converted to buffer coordinates.
847 *
848 * Buffer_region must be init'd, but will be completely overwritten.
849 *
850 * Viewport and buffer transformations can only do translation, scaling,
851 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600852 * conversion is from the coordinate rounding that takes place in
853 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200854 */
855WL_EXPORT void
856weston_surface_to_buffer_region(struct weston_surface *surface,
857 pixman_region32_t *surface_region,
858 pixman_region32_t *buffer_region)
859{
860 pixman_box32_t *src_rects, *dest_rects;
861 int nrects, i;
862
863 src_rects = pixman_region32_rectangles(surface_region, &nrects);
864 dest_rects = malloc(nrects * sizeof(*dest_rects));
865 if (!dest_rects)
866 return;
867
868 for (i = 0; i < nrects; i++) {
869 dest_rects[i] = weston_surface_to_buffer_rect(surface,
870 src_rects[i]);
871 }
872
873 pixman_region32_fini(buffer_region);
874 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
875 free(dest_rects);
876}
877
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200878WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400880 struct weston_plane *plane)
881{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500882 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400883 return;
884
Jason Ekstranda7af7042013-10-12 22:38:11 -0500885 weston_view_damage_below(view);
886 view->plane = plane;
887 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400888}
889
Pekka Paalanen51723d52015-02-17 13:10:01 +0200890/** Inflict damage on the plane where the view is visible.
891 *
892 * \param view The view that causes the damage.
893 *
894 * If the view is currently on a plane (including the primary plane),
895 * take the view's boundingbox, subtract all the opaque views that cover it,
896 * and add the remaining region as damage to the plane. This corresponds
897 * to the damage inflicted to the plane if this view disappeared.
898 *
899 * A repaint is scheduled for this view.
900 *
901 * The region of all opaque views covering this view is stored in
902 * weston_view::clip and updated by view_accumulate_damage() during
903 * weston_output_repaint(). Specifically, that region matches the
904 * scenegraph as it was last painted.
905 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400906WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500907weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200908{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500909 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200910
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500911 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200912 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500913 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800914 if (view->plane)
915 pixman_region32_union(&view->plane->damage,
916 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500917 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800918 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200919}
920
Bryce Harrington3f650b82015-12-23 11:01:58 -0800921/**
922 * \param es The surface
923 * \param mask The new set of outputs for the surface
924 *
925 * Sets the surface's set of outputs to the ones specified by
926 * the new output mask provided. Identifies the outputs that
927 * have changed, the posts enter and leave events for these
928 * outputs as appropriate.
929 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400930static void
931weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
932{
933 uint32_t different = es->output_mask ^ mask;
934 uint32_t entered = mask & different;
935 uint32_t left = es->output_mask & different;
936 struct weston_output *output;
937 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500938 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400939
940 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500941 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400942 return;
943 if (different == 0)
944 return;
945
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500946 client = wl_resource_get_client(es->resource);
947
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400948 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800949 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400950 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500951 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400952 client);
953 if (resource == NULL)
954 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800955 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500956 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800957 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500958 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400959 }
960}
961
Bryce Harrington3f650b82015-12-23 11:01:58 -0800962/** Recalculate which output(s) the surface has views displayed on
963 *
964 * \param es The surface to remap to outputs
965 *
966 * Finds the output that is showing the largest amount of one
967 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300968 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800969 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300970 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800971 * in the output_mask for the surface.
972 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400973static void
974weston_surface_assign_output(struct weston_surface *es)
975{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 struct weston_output *new_output;
977 struct weston_view *view;
978 pixman_region32_t region;
979 uint32_t max, area, mask;
980 pixman_box32_t *e;
981
982 new_output = NULL;
983 max = 0;
984 mask = 0;
985 pixman_region32_init(&region);
986 wl_list_for_each(view, &es->views, surface_link) {
987 if (!view->output)
988 continue;
989
990 pixman_region32_intersect(&region, &view->transform.boundingbox,
991 &view->output->region);
992
993 e = pixman_region32_extents(&region);
994 area = (e->x2 - e->x1) * (e->y2 - e->y1);
995
996 mask |= view->output_mask;
997
998 if (area >= max) {
999 new_output = view->output;
1000 max = area;
1001 }
1002 }
1003 pixman_region32_fini(&region);
1004
1005 es->output = new_output;
1006 weston_surface_update_output_mask(es, mask);
1007}
1008
Bryce Harrington3f650b82015-12-23 11:01:58 -08001009/** Recalculate which output(s) the view is displayed on
1010 *
1011 * \param ev The view to remap to outputs
1012 *
1013 * Identifies the set of outputs that the view is visible on,
1014 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001015 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001016 *
1017 * Also does the same for the view's surface. See
1018 * weston_surface_assign_output().
1019 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020static void
1021weston_view_assign_output(struct weston_view *ev)
1022{
1023 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001024 struct weston_output *output, *new_output;
1025 pixman_region32_t region;
1026 uint32_t max, area, mask;
1027 pixman_box32_t *e;
1028
1029 new_output = NULL;
1030 max = 0;
1031 mask = 0;
1032 pixman_region32_init(&region);
1033 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001034 if (output->destroying)
1035 continue;
1036
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001038 &output->region);
1039
1040 e = pixman_region32_extents(&region);
1041 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1042
1043 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001044 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001045
1046 if (area >= max) {
1047 new_output = output;
1048 max = area;
1049 }
1050 }
1051 pixman_region32_fini(&region);
1052
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 ev->output = new_output;
1054 ev->output_mask = mask;
1055
1056 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001057}
1058
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001059static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001060weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1061 int from_x, int from_y, int *to_x, int *to_y)
1062{
1063 float x, y;
1064
1065 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1066 weston_view_from_global_float(to, x, y, &x, &y);
1067
1068 *to_x = round(x);
1069 *to_y = round(y);
1070}
1071
1072static void
1073weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1074{
1075 pixman_box32_t *a;
1076 pixman_box32_t b;
1077
1078 a = pixman_region32_extents(&from->geometry.scissor);
1079
1080 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1081 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1082
1083 pixman_region32_fini(&to->geometry.scissor);
1084 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1085}
1086
1087static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001088view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001090{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001091 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1092 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001093 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001094 { inbox->x1, inbox->y1 },
1095 { inbox->x1, inbox->y2 },
1096 { inbox->x2, inbox->y1 },
1097 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001098 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001099 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001100 int i;
1101
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001102 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001103 /* avoid rounding empty bbox to 1x1 */
1104 pixman_region32_init(bbox);
1105 return;
1106 }
1107
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001108 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001109 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001111 if (x < min_x)
1112 min_x = x;
1113 if (x > max_x)
1114 max_x = x;
1115 if (y < min_y)
1116 min_y = y;
1117 if (y > max_y)
1118 max_y = y;
1119 }
1120
Pekka Paalanen219b9822012-02-08 15:38:37 +02001121 int_x = floorf(min_x);
1122 int_y = floorf(min_y);
1123 pixman_region32_init_rect(bbox, int_x, int_y,
1124 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001125}
1126
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001127static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001129{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001131
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001132 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 view->geometry.x = roundf(view->geometry.x);
1134 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001135
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001136 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1138 view->transform.position.matrix.d[12] = view->geometry.x;
1139 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001140
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001142
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143 view->transform.inverse = view->transform.position.matrix;
1144 view->transform.inverse.d[12] = -view->geometry.x;
1145 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001146
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001148 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001149 view->surface->width,
1150 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001151 if (view->geometry.scissor_enabled)
1152 pixman_region32_intersect(&view->transform.boundingbox,
1153 &view->transform.boundingbox,
1154 &view->geometry.scissor);
1155
1156 pixman_region32_translate(&view->transform.boundingbox,
1157 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001158
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 if (view->alpha == 1.0) {
1160 pixman_region32_copy(&view->transform.opaque,
1161 &view->surface->opaque);
1162 pixman_region32_translate(&view->transform.opaque,
1163 view->geometry.x,
1164 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001165 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001166}
1167
1168static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001170{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 struct weston_view *parent = view->geometry.parent;
1172 struct weston_matrix *matrix = &view->transform.matrix;
1173 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001174 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001175 pixman_region32_t surfregion;
1176 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001177
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001179
1180 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1182 view->transform.position.matrix.d[12] = view->geometry.x;
1183 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001184
1185 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001187 weston_matrix_multiply(matrix, &tform->matrix);
1188
Pekka Paalanen483243f2013-03-08 14:56:50 +02001189 if (parent)
1190 weston_matrix_multiply(matrix, &parent->transform.matrix);
1191
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001192 if (weston_matrix_invert(inverse, matrix) < 0) {
1193 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 weston_log("error: weston_view %p"
1195 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001196 return -1;
1197 }
1198
Pekka Paalanen380adf52015-02-16 14:39:11 +02001199 pixman_region32_init_rect(&surfregion, 0, 0,
1200 view->surface->width, view->surface->height);
1201 if (view->geometry.scissor_enabled)
1202 pixman_region32_intersect(&surfregion, &surfregion,
1203 &view->geometry.scissor);
1204 surfbox = pixman_region32_extents(&surfregion);
1205
1206 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1207 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001208
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001209 return 0;
1210}
1211
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001212static struct weston_layer *
1213get_view_layer(struct weston_view *view)
1214{
1215 if (view->parent_view)
1216 return get_view_layer(view->parent_view);
1217 return view->layer_link.layer;
1218}
1219
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001220WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001222{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001224 struct weston_layer *layer;
1225 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001226
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001228 return;
1229
Pekka Paalanen483243f2013-03-08 14:56:50 +02001230 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001232
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001234
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001236
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 pixman_region32_fini(&view->transform.boundingbox);
1238 pixman_region32_fini(&view->transform.opaque);
1239 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001240
Pekka Paalanencd403622012-01-25 13:37:39 +02001241 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 if (view->geometry.transformation_list.next ==
1243 &view->transform.position.link &&
1244 view->geometry.transformation_list.prev ==
1245 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001246 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001248 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 if (weston_view_update_transform_enable(view) < 0)
1250 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001251 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001252
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001253 layer = get_view_layer(view);
1254 if (layer) {
1255 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001256 pixman_region32_intersect(&view->transform.boundingbox,
1257 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001258 pixman_region32_intersect(&view->transform.opaque,
1259 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001260 pixman_region32_fini(&mask);
1261 }
1262
Pekka Paalanen380adf52015-02-16 14:39:11 +02001263 if (parent) {
1264 if (parent->geometry.scissor_enabled) {
1265 view->geometry.scissor_enabled = true;
1266 weston_view_transfer_scissor(parent, view);
1267 } else {
1268 view->geometry.scissor_enabled = false;
1269 }
1270 }
1271
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001273
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001275
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 wl_signal_emit(&view->surface->compositor->transform_signal,
1277 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001278}
1279
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001280WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001282{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001284
1285 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 * The invariant: if view->geometry.dirty, then all views
1287 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001288 * Corollary: if not parent->geometry.dirty, then all ancestors
1289 * are not dirty.
1290 */
1291
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001293 return;
1294
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001296
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001298 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001300}
1301
1302WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303weston_view_to_global_fixed(struct weston_view *view,
1304 wl_fixed_t vx, wl_fixed_t vy,
1305 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001306{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001307 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001308
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 weston_view_to_global_float(view,
1310 wl_fixed_to_double(vx),
1311 wl_fixed_to_double(vy),
1312 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001313 *x = wl_fixed_from_double(xf);
1314 *y = wl_fixed_from_double(yf);
1315}
1316
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001317WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318weston_view_from_global_float(struct weston_view *view,
1319 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001320{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001322 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1323
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001325
1326 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001327 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001329 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 *vx = 0;
1331 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001332 return;
1333 }
1334
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 *vx = v.f[0] / v.f[3];
1336 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001337 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338 *vx = x - view->geometry.x;
1339 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001340 }
1341}
1342
1343WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344weston_view_from_global_fixed(struct weston_view *view,
1345 wl_fixed_t x, wl_fixed_t y,
1346 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001347{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001349
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350 weston_view_from_global_float(view,
1351 wl_fixed_to_double(x),
1352 wl_fixed_to_double(y),
1353 &vxf, &vyf);
1354 *vx = wl_fixed_from_double(vxf);
1355 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001356}
1357
1358WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359weston_view_from_global(struct weston_view *view,
1360 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001361{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001362 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001363
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1365 *vx = floorf(vxf);
1366 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001367}
1368
Bryce Harrington3f650b82015-12-23 11:01:58 -08001369/**
1370 * \param surface The surface to be repainted
1371 *
1372 * Marks the output(s) that the surface is shown on as needing to be
1373 * repainted. See weston_output_schedule_repaint().
1374 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001375WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001376weston_surface_schedule_repaint(struct weston_surface *surface)
1377{
1378 struct weston_output *output;
1379
1380 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001381 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001382 weston_output_schedule_repaint(output);
1383}
1384
Bryce Harrington3f650b82015-12-23 11:01:58 -08001385/**
1386 * \param view The view to be repainted
1387 *
1388 * Marks the output(s) that the view is shown on as needing to be
1389 * repainted. See weston_output_schedule_repaint().
1390 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001391WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392weston_view_schedule_repaint(struct weston_view *view)
1393{
1394 struct weston_output *output;
1395
1396 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001397 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 weston_output_schedule_repaint(output);
1399}
1400
Pekka Paalanene508ce62015-02-19 13:59:55 +02001401/**
1402 * XXX: This function does it the wrong way.
1403 * surface->damage is the damage from the client, and causes
1404 * surface_flush_damage() to copy pixels. No window management action can
1405 * cause damage to the client-provided content, warranting re-upload!
1406 *
1407 * Instead of surface->damage, this function should record the damage
1408 * with all the views for this surface to avoid extraneous texture
1409 * uploads.
1410 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001412weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001413{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001414 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415 0, 0, surface->width,
1416 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001417
Kristian Høgsberg98238702012-08-03 16:29:12 -04001418 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001419}
1420
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001421WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001423{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001424 if (view->geometry.x == x && view->geometry.y == y)
1425 return;
1426
Jason Ekstranda7af7042013-10-12 22:38:11 -05001427 view->geometry.x = x;
1428 view->geometry.y = y;
1429 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001430}
1431
Pekka Paalanen483243f2013-03-08 14:56:50 +02001432static void
1433transform_parent_handle_parent_destroy(struct wl_listener *listener,
1434 void *data)
1435{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 struct weston_view *view =
1437 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001438 geometry.parent_destroy_listener);
1439
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001441}
1442
1443WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001444weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001445 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001446{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 if (view->geometry.parent) {
1448 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1449 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001450
1451 if (!parent)
1452 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001453 }
1454
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001456
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001458 transform_parent_handle_parent_destroy;
1459 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001460 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001462 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001464 }
1465
Jason Ekstranda7af7042013-10-12 22:38:11 -05001466 weston_view_geometry_dirty(view);
1467}
1468
Pekka Paalanen380adf52015-02-16 14:39:11 +02001469/** Set a clip mask rectangle on a view
1470 *
1471 * \param view The view to set the clip mask on.
1472 * \param x Top-left corner X coordinate of the clip rectangle.
1473 * \param y Top-left corner Y coordinate of the clip rectangle.
1474 * \param width Width of the clip rectangle, non-negative.
1475 * \param height Height of the clip rectangle, non-negative.
1476 *
1477 * A shell may set a clip mask rectangle on a view. Everything outside
1478 * the rectangle is cut away for input and output purposes: it is
1479 * not drawn and cannot be hit by hit-test based input like pointer
1480 * motion or touch-downs. Everything inside the rectangle will behave
1481 * normally. Clients are unaware of clipping.
1482 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001483 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001484 * mask rectangle does not affect the view position, the view is positioned
1485 * as it would be without a clip. The clip also does not change
1486 * weston_surface::width,height.
1487 *
1488 * The clip mask rectangle is part of transformation inheritance
1489 * (weston_view_set_transform_parent()). A clip set in the root of the
1490 * transformation inheritance tree will affect all views in the tree.
1491 * A clip can be set only on the root view. Attempting to set a clip
1492 * on view that has a transformation parent will fail. Assigning a parent
1493 * to a view that has a clip set will cause the clip to be forgotten.
1494 *
1495 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1496 * on the additional transformations in the child views. These transformations
1497 * may not rotate the coordinate axes, i.e., only translation and scaling
1498 * are allowed. Violating this restriction causes the clipping to malfunction.
1499 * Furthermore, using scaling may cause rounding errors in child clipping.
1500 *
1501 * The clip mask rectangle is not automatically adjusted based on
1502 * wl_surface.attach dx and dy arguments.
1503 *
1504 * A clip mask rectangle can be set only if the compositor capability
1505 * WESTON_CAP_VIEW_CLIP_MASK is present.
1506 *
1507 * This function sets the clip mask rectangle and schedules a repaint for
1508 * the view.
1509 */
1510WL_EXPORT void
1511weston_view_set_mask(struct weston_view *view,
1512 int x, int y, int width, int height)
1513{
1514 struct weston_compositor *compositor = view->surface->compositor;
1515
1516 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1517 weston_log("%s not allowed without capability!\n", __func__);
1518 return;
1519 }
1520
1521 if (view->geometry.parent) {
1522 weston_log("view %p has a parent, clip forbidden!\n", view);
1523 return;
1524 }
1525
1526 if (width < 0 || height < 0) {
1527 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1528 x, y, width, height);
1529 return;
1530 }
1531
1532 pixman_region32_fini(&view->geometry.scissor);
1533 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1534 view->geometry.scissor_enabled = true;
1535 weston_view_geometry_dirty(view);
1536 weston_view_schedule_repaint(view);
1537}
1538
1539/** Remove the clip mask from a view
1540 *
1541 * \param view The view to remove the clip mask from.
1542 *
1543 * Removed the clip mask rectangle and schedules a repaint.
1544 *
1545 * \sa weston_view_set_mask
1546 */
1547WL_EXPORT void
1548weston_view_set_mask_infinite(struct weston_view *view)
1549{
1550 view->geometry.scissor_enabled = false;
1551 weston_view_geometry_dirty(view);
1552 weston_view_schedule_repaint(view);
1553}
1554
Armin Krezović0da12b82016-06-30 06:04:33 +02001555/* Check if view should be displayed
1556 *
1557 * The indicator is set manually when assigning
1558 * a view to a surface.
1559 *
1560 * This needs reworking. See the thread starting at:
1561 *
1562 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1563 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001564WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565weston_view_is_mapped(struct weston_view *view)
1566{
Armin Krezović0da12b82016-06-30 06:04:33 +02001567 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001568}
1569
Armin Krezović0da12b82016-06-30 06:04:33 +02001570/* Check if a surface has a view assigned to it
1571 *
1572 * The indicator is set manually when mapping
1573 * a surface and creating a view for it.
1574 *
1575 * This needs to go. See the thread starting at:
1576 *
1577 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1578 *
1579 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001580WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001581weston_surface_is_mapped(struct weston_surface *surface)
1582{
Armin Krezović0da12b82016-06-30 06:04:33 +02001583 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001584}
1585
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001586static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001587surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001588{
1589 struct weston_view *view;
1590
1591 if (surface->width == width && surface->height == height)
1592 return;
1593
1594 surface->width = width;
1595 surface->height = height;
1596
1597 wl_list_for_each(view, &surface->views, surface_link)
1598 weston_view_geometry_dirty(view);
1599}
1600
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001601WL_EXPORT void
1602weston_surface_set_size(struct weston_surface *surface,
1603 int32_t width, int32_t height)
1604{
1605 assert(!surface->resource);
1606 surface_set_size(surface, width, height);
1607}
1608
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001609static int
1610fixed_round_up_to_int(wl_fixed_t f)
1611{
1612 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1613}
1614
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001615static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001616convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1617 int32_t width, int32_t height,
1618 uint32_t transform,
1619 int32_t scale)
1620{
1621 assert(scale > 0);
1622
1623 switch (transform) {
1624 case WL_OUTPUT_TRANSFORM_NORMAL:
1625 case WL_OUTPUT_TRANSFORM_180:
1626 case WL_OUTPUT_TRANSFORM_FLIPPED:
1627 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1628 *width_out = width / scale;
1629 *height_out = height / scale;
1630 break;
1631 case WL_OUTPUT_TRANSFORM_90:
1632 case WL_OUTPUT_TRANSFORM_270:
1633 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1634 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1635 *width_out = height / scale;
1636 *height_out = width / scale;
1637 break;
1638 default:
1639 assert(0 && "invalid transform");
1640 }
1641}
1642
1643static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001644weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001645{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001646 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001647
1648 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001649 surface->width_from_buffer = 0;
1650 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001651 return;
1652 }
1653
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001654 convert_size_by_transform_scale(&surface->width_from_buffer,
1655 &surface->height_from_buffer,
1656 surface->buffer_ref.buffer->width,
1657 surface->buffer_ref.buffer->height,
1658 vp->buffer.transform,
1659 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001660}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001661
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001662static void
1663weston_surface_update_size(struct weston_surface *surface)
1664{
1665 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1666 int32_t width, height;
1667
1668 width = surface->width_from_buffer;
1669 height = surface->height_from_buffer;
1670
1671 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001672 surface_set_size(surface,
1673 vp->surface.width, vp->surface.height);
1674 return;
1675 }
1676
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001677 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001678 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1679 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1680
1681 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001682 return;
1683 }
1684
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001685 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001686}
1687
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001688WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001689weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001690{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001691 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001692
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001693 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001694
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001695 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001696}
1697
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698WL_EXPORT struct weston_view *
1699weston_compositor_pick_view(struct weston_compositor *compositor,
1700 wl_fixed_t x, wl_fixed_t y,
1701 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001702{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001703 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001704 wl_fixed_t view_x, view_y;
1705 int view_ix, view_iy;
1706 int ix = wl_fixed_to_int(x);
1707 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001708
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001710 if (!pixman_region32_contains_point(
1711 &view->transform.boundingbox, ix, iy, NULL))
1712 continue;
1713
1714 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1715 view_ix = wl_fixed_to_int(view_x);
1716 view_iy = wl_fixed_to_int(view_y);
1717
1718 if (!pixman_region32_contains_point(&view->surface->input,
1719 view_ix, view_iy, NULL))
1720 continue;
1721
Pekka Paalanen380adf52015-02-16 14:39:11 +02001722 if (view->geometry.scissor_enabled &&
1723 !pixman_region32_contains_point(&view->geometry.scissor,
1724 view_ix, view_iy, NULL))
1725 continue;
1726
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001727 *vx = view_x;
1728 *vy = view_y;
1729 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001730 }
1731
Derek Foremanf9318d12015-05-11 15:40:11 -05001732 *vx = wl_fixed_from_int(-1000000);
1733 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001734 return NULL;
1735}
1736
1737static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001738weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001739{
Daniel Stone37816df2012-05-16 18:45:18 +01001740 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001741
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001742 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001743 return;
1744
Daniel Stone37816df2012-05-16 18:45:18 +01001745 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001746 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001747}
1748
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001749WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001751{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001752 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001753
Jason Ekstranda7af7042013-10-12 22:38:11 -05001754 if (!weston_view_is_mapped(view))
1755 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001756
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757 weston_view_damage_below(view);
1758 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001759 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001760 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001761 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762 wl_list_remove(&view->link);
1763 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001764 view->output_mask = 0;
1765 weston_surface_assign_output(view->surface);
1766
1767 if (weston_surface_is_mapped(view->surface))
1768 return;
1769
1770 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001771 struct weston_touch *touch = weston_seat_get_touch(seat);
1772 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1773 struct weston_keyboard *keyboard =
1774 weston_seat_get_keyboard(seat);
1775
1776 if (keyboard && keyboard->focus == view->surface)
1777 weston_keyboard_set_focus(keyboard, NULL);
1778 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001779 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001780 if (touch && touch->focus == view)
1781 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001782 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001784
Jason Ekstranda7af7042013-10-12 22:38:11 -05001785WL_EXPORT void
1786weston_surface_unmap(struct weston_surface *surface)
1787{
1788 struct weston_view *view;
1789
Armin Krezovićf8486c32016-06-30 06:04:28 +02001790 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791 wl_list_for_each(view, &surface->views, surface_link)
1792 weston_view_unmap(view);
1793 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001794}
1795
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001796static void
1797weston_surface_reset_pending_buffer(struct weston_surface *surface)
1798{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001799 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001800 surface->pending.sx = 0;
1801 surface->pending.sy = 0;
1802 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001803 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001804}
1805
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806WL_EXPORT void
1807weston_view_destroy(struct weston_view *view)
1808{
1809 wl_signal_emit(&view->destroy_signal, view);
1810
1811 assert(wl_list_empty(&view->geometry.child_list));
1812
1813 if (weston_view_is_mapped(view)) {
1814 weston_view_unmap(view);
1815 weston_compositor_build_view_list(view->surface->compositor);
1816 }
1817
1818 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001819 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001820
1821 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001822 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001823 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001824 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825
1826 weston_view_set_transform_parent(view, NULL);
1827
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 wl_list_remove(&view->surface_link);
1829
1830 free(view);
1831}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001832
1833WL_EXPORT void
1834weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001835{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001836 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001837 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001838 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001839
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001840 if (--surface->ref_count > 0)
1841 return;
1842
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001843 assert(surface->resource == NULL);
1844
Pekka Paalanenca790762015-04-17 14:23:38 +03001845 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001846
Pekka Paalanene67858b2013-04-25 13:57:42 +03001847 assert(wl_list_empty(&surface->subsurface_list_pending));
1848 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001849
Jason Ekstranda7af7042013-10-12 22:38:11 -05001850 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1851 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001852
Jason Ekstrand7b982072014-05-20 14:33:03 -05001853 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001854
Pekka Paalanende685b82012-12-04 15:58:12 +02001855 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001856
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001857 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001858 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001859 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001860
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001861 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001862 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001863
Pekka Paalanen133e4392014-09-23 22:08:46 -04001864 weston_presentation_feedback_discard_list(&surface->feedback_list);
1865
Jonas Ådahld3414f22016-07-22 17:56:31 +08001866 wl_list_for_each_safe(constraint, next_constraint,
1867 &surface->pointer_constraints,
1868 link)
1869 weston_pointer_constraint_destroy(constraint);
1870
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001871 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001872}
1873
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001874static void
1875destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001876{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001877 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001878
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001879 assert(surface);
1880
Giulio Camuffo0d379742013-11-15 22:06:15 +01001881 /* Set the resource to NULL, since we don't want to leave a
1882 * dangling pointer if the surface was refcounted and survives
1883 * the weston_surface_destroy() call. */
1884 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001885
1886 if (surface->viewport_resource)
1887 wl_resource_set_user_data(surface->viewport_resource, NULL);
1888
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001889 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001890}
1891
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001892static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001893weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1894{
1895 struct weston_buffer *buffer =
1896 container_of(listener, struct weston_buffer, destroy_listener);
1897
1898 wl_signal_emit(&buffer->destroy_signal, buffer);
1899 free(buffer);
1900}
1901
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001902WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001903weston_buffer_from_resource(struct wl_resource *resource)
1904{
1905 struct weston_buffer *buffer;
1906 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001907
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001908 listener = wl_resource_get_destroy_listener(resource,
1909 weston_buffer_destroy_handler);
1910
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001911 if (listener)
1912 return container_of(listener, struct weston_buffer,
1913 destroy_listener);
1914
1915 buffer = zalloc(sizeof *buffer);
1916 if (buffer == NULL)
1917 return NULL;
1918
1919 buffer->resource = resource;
1920 wl_signal_init(&buffer->destroy_signal);
1921 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001922 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001923 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001924
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001925 return buffer;
1926}
1927
1928static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001929weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1930 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001931{
Pekka Paalanende685b82012-12-04 15:58:12 +02001932 struct weston_buffer_reference *ref =
1933 container_of(listener, struct weston_buffer_reference,
1934 destroy_listener);
1935
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001936 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001937 ref->buffer = NULL;
1938}
1939
1940WL_EXPORT void
1941weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001942 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001943{
1944 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001945 ref->buffer->busy_count--;
1946 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001947 assert(wl_resource_get_client(ref->buffer->resource));
1948 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001949 WL_BUFFER_RELEASE);
1950 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001951 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001952 }
1953
Pekka Paalanende685b82012-12-04 15:58:12 +02001954 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001955 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001956 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001957 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001958 }
1959
Pekka Paalanende685b82012-12-04 15:58:12 +02001960 ref->buffer = buffer;
1961 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1962}
1963
1964static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001965weston_surface_attach(struct weston_surface *surface,
1966 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001967{
1968 weston_buffer_reference(&surface->buffer_ref, buffer);
1969
Pekka Paalanena6421c42012-12-04 15:58:10 +02001970 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001971 if (weston_surface_is_mapped(surface))
1972 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001973 }
1974
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001975 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001976
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001977 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001978 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001979}
1980
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001981WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001982weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001983{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001984 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001985
1986 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001987 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001988}
1989
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001990WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001991weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001992{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001993 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001994
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001995 pixman_region32_union(&compositor->primary_plane.damage,
1996 &compositor->primary_plane.damage,
1997 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001998 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001999}
2000
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002001static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002002surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002003{
Pekka Paalanende685b82012-12-04 15:58:12 +02002004 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002005 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002006 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002007
Pekka Paalanenb5026542014-11-12 15:09:24 +02002008 if (weston_timeline_enabled_ &&
2009 pixman_region32_not_empty(&surface->damage))
2010 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2011 TLP_OUTPUT(surface->output), TLP_END);
2012
Jason Ekstrandef540082014-06-26 10:37:36 -07002013 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002014}
2015
2016static void
2017view_accumulate_damage(struct weston_view *view,
2018 pixman_region32_t *opaque)
2019{
2020 pixman_region32_t damage;
2021
2022 pixman_region32_init(&damage);
2023 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002024 pixman_box32_t *extents;
2025
Jason Ekstranda7af7042013-10-12 22:38:11 -05002026 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002027 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002028 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002029 pixman_region32_copy(&damage, &view->surface->damage);
2030 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002031 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002032 }
2033
Pekka Paalanen380adf52015-02-16 14:39:11 +02002034 pixman_region32_intersect(&damage, &damage,
2035 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002036 pixman_region32_subtract(&damage, &damage, opaque);
2037 pixman_region32_union(&view->plane->damage,
2038 &view->plane->damage, &damage);
2039 pixman_region32_fini(&damage);
2040 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002041 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002042}
2043
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002044static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002045compositor_accumulate_damage(struct weston_compositor *ec)
2046{
2047 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002048 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002049 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002050
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002051 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002052
2053 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002054 pixman_region32_copy(&plane->clip, &clip);
2055
2056 pixman_region32_init(&opaque);
2057
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058 wl_list_for_each(ev, &ec->view_list, link) {
2059 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002060 continue;
2061
Jason Ekstranda7af7042013-10-12 22:38:11 -05002062 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002063 }
2064
2065 pixman_region32_union(&clip, &clip, &opaque);
2066 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002067 }
2068
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002069 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002070
Jason Ekstranda7af7042013-10-12 22:38:11 -05002071 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002072 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002073
2074 wl_list_for_each(ev, &ec->view_list, link) {
2075 if (ev->surface->touched)
2076 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002077 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002078
2079 surface_flush_damage(ev->surface);
2080
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002081 /* Both the renderer and the backend have seen the buffer
2082 * by now. If renderer needs the buffer, it has its own
2083 * reference set. If the backend wants to keep the buffer
2084 * around for migrating the surface into a non-primary plane
2085 * later, keep_buffer is true. Otherwise, drop the core
2086 * reference now, and allow early buffer release. This enables
2087 * clients to use single-buffering.
2088 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089 if (!ev->surface->keep_buffer)
2090 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002091 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002092}
2093
2094static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002095surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002096{
2097 struct weston_subsurface *sub;
2098
Pekka Paalanene67858b2013-04-25 13:57:42 +03002099 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002101 continue;
2102
Jason Ekstranda7af7042013-10-12 22:38:11 -05002103 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2104 wl_list_init(&sub->surface->views);
2105
2106 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002107 }
2108}
2109
2110static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002111surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002112{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002113 struct weston_subsurface *sub;
2114 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002115
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2117 if (sub->surface == surface)
2118 continue;
2119
George Kiagiadakised04d382014-06-13 18:10:26 +02002120 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2121 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002122 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002123 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124
2125 surface_free_unused_subsurface_views(sub->surface);
2126 }
2127}
2128
2129static void
2130view_list_add_subsurface_view(struct weston_compositor *compositor,
2131 struct weston_subsurface *sub,
2132 struct weston_view *parent)
2133{
2134 struct weston_subsurface *child;
2135 struct weston_view *view = NULL, *iv;
2136
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002137 if (!weston_surface_is_mapped(sub->surface))
2138 return;
2139
Jason Ekstranda7af7042013-10-12 22:38:11 -05002140 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2141 if (iv->geometry.parent == parent) {
2142 view = iv;
2143 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002144 }
2145 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002146
2147 if (view) {
2148 /* Put it back in the surface's list of views */
2149 wl_list_remove(&view->surface_link);
2150 wl_list_insert(&sub->surface->views, &view->surface_link);
2151 } else {
2152 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002153 weston_view_set_position(view,
2154 sub->position.x,
2155 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002156 weston_view_set_transform_parent(view, parent);
2157 }
2158
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002159 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002160 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002161 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002162
Pekka Paalanenb188e912013-11-19 14:03:35 +02002163 if (wl_list_empty(&sub->surface->subsurface_list)) {
2164 wl_list_insert(compositor->view_list.prev, &view->link);
2165 return;
2166 }
2167
2168 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2169 if (child->surface == sub->surface)
2170 wl_list_insert(compositor->view_list.prev, &view->link);
2171 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002173 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002174}
2175
2176static void
2177view_list_add(struct weston_compositor *compositor,
2178 struct weston_view *view)
2179{
2180 struct weston_subsurface *sub;
2181
2182 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002183
Pekka Paalanenb188e912013-11-19 14:03:35 +02002184 if (wl_list_empty(&view->surface->subsurface_list)) {
2185 wl_list_insert(compositor->view_list.prev, &view->link);
2186 return;
2187 }
2188
2189 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2190 if (sub->surface == view->surface)
2191 wl_list_insert(compositor->view_list.prev, &view->link);
2192 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002193 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002194 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002195}
2196
2197static void
2198weston_compositor_build_view_list(struct weston_compositor *compositor)
2199{
2200 struct weston_view *view;
2201 struct weston_layer *layer;
2202
2203 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002204 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002205 surface_stash_subsurface_views(view->surface);
2206
2207 wl_list_init(&compositor->view_list);
2208 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002209 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002210 view_list_add(compositor, view);
2211 }
2212 }
2213
2214 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002215 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002216 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002217}
2218
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002219static void
2220weston_output_take_feedback_list(struct weston_output *output,
2221 struct weston_surface *surface)
2222{
2223 struct weston_view *view;
2224 struct weston_presentation_feedback *feedback;
2225 uint32_t flags = 0xffffffff;
2226
2227 if (wl_list_empty(&surface->feedback_list))
2228 return;
2229
2230 /* All views must have the flag for the flag to survive. */
2231 wl_list_for_each(view, &surface->views, surface_link) {
2232 /* ignore views that are not on this output at all */
2233 if (view->output_mask & (1u << output->id))
2234 flags &= view->psf_flags;
2235 }
2236
2237 wl_list_for_each(feedback, &surface->feedback_list, link)
2238 feedback->psf_flags = flags;
2239
2240 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2241 wl_list_init(&surface->feedback_list);
2242}
2243
David Herrmann1edf44c2013-10-22 17:11:26 +02002244static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002245weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002246{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002247 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002248 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002249 struct weston_animation *animation, *next;
2250 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002251 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002252 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002253 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002254
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002255 if (output->destroying)
2256 return 0;
2257
Pekka Paalanenb5026542014-11-12 15:09:24 +02002258 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2259
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002260 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002261 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002262
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002263 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002264 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002265 } else {
2266 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002268 ev->psf_flags = 0;
2269 }
2270 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002271
Pekka Paalanene67858b2013-04-25 13:57:42 +03002272 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002273 wl_list_for_each(ev, &ec->view_list, link) {
2274 /* Note: This operation is safe to do multiple times on the
2275 * same surface.
2276 */
2277 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002278 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002279 &ev->surface->frame_callback_list);
2280 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002281
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002282 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002283 }
2284 }
2285
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002286 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002287
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002288 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002289 pixman_region32_intersect(&output_damage,
2290 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002291 pixman_region32_subtract(&output_damage,
2292 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002293
Scott Moreauccbf29d2012-02-22 14:21:41 -07002294 if (output->dirty)
2295 weston_output_update_matrix(output);
2296
David Herrmann1edf44c2013-10-22 17:11:26 +02002297 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002298
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002299 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002300
Kristian Høgsbergef044142011-06-21 15:02:12 -04002301 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002302
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002303 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002304
Jonas Ådahldb773762012-06-13 00:01:21 +02002305 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002306 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002307 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002308 }
2309
Scott Moreaud64cf212012-06-08 19:40:54 -06002310 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002311 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002312 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002313 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002314
Pekka Paalanenb5026542014-11-12 15:09:24 +02002315 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2316
David Herrmann1edf44c2013-10-22 17:11:26 +02002317 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002318}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002319
Pekka Paalanen82919792014-05-21 13:51:49 +03002320static void
2321weston_output_schedule_repaint_reset(struct weston_output *output)
2322{
Pekka Paalanen82919792014-05-21 13:51:49 +03002323 output->repaint_scheduled = 0;
2324 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002325}
2326
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002327/** Retrieves a mask of outputs that should inhibit screensaving
2328 *
2329 * \param compositor The compositor instance.
2330 * \return An output mask indicating the ids of all inhibiting outputs
2331 *
2332 * Checks for surfaces whose clients have requested that they
2333 * disable the screenserver and display powersaving. Note
2334 * the output ids for these surfaces.
2335 */
2336WL_EXPORT uint32_t
2337weston_compositor_inhibited_outputs(struct weston_compositor *compositor)
2338{
2339 struct weston_view *view;
2340 uint32_t inhibited_outputs_mask = 0;
2341
2342 wl_list_for_each(view, &compositor->view_list, link) {
2343 /* Does the view's surface inhibit this output? */
2344 if (!view->surface->inhibit_idling)
2345 continue;
2346
2347 inhibited_outputs_mask |= view->output_mask;
2348 }
2349 return inhibited_outputs_mask;
2350}
2351
Pekka Paalanen0513a952014-05-21 16:17:27 +03002352static int
2353output_repaint_timer_handler(void *data)
2354{
2355 struct weston_output *output = data;
2356 struct weston_compositor *compositor = output->compositor;
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002357 uint32_t inhibited_outputs_mask = weston_compositor_inhibited_outputs(compositor);
Pekka Paalanen0513a952014-05-21 16:17:27 +03002358
2359 if (output->repaint_needed &&
Pekka Paalanen0513a952014-05-21 16:17:27 +03002360 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002361 (compositor->state != WESTON_COMPOSITOR_SLEEPING
2362 || inhibited_outputs_mask & (1 << output->id)) &&
Pekka Paalanen0513a952014-05-21 16:17:27 +03002363 weston_output_repaint(output) == 0)
2364 return 0;
2365
2366 weston_output_schedule_repaint_reset(output);
2367
2368 return 0;
2369}
2370
Kristian Høgsbergef044142011-06-21 15:02:12 -04002371WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002372weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002373 const struct timespec *stamp,
2374 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002375{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002376 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002377 int32_t refresh_nsec;
2378 struct timespec now;
2379 struct timespec gone;
2380 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002381
Pekka Paalanenb5026542014-11-12 15:09:24 +02002382 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2383 TLP_VBLANK(stamp), TLP_END);
2384
Pekka Paalanend7894d02015-07-03 15:08:53 +03002385 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002386 weston_presentation_feedback_present_list(&output->feedback_list,
2387 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002388 output->msc,
2389 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002390
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002391 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002392
Pekka Paalanen0513a952014-05-21 16:17:27 +03002393 weston_compositor_read_presentation_clock(compositor, &now);
2394 timespec_sub(&gone, &now, stamp);
2395 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2396 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002397
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002398 if (msec < -1000 || msec > 1000) {
2399 static bool warned;
2400
2401 if (!warned)
2402 weston_log("Warning: computed repaint delay is "
2403 "insane: %d msec\n", msec);
2404 warned = true;
2405
2406 msec = 0;
2407 }
2408
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002409 /* Called from restart_repaint_loop and restart happens already after
2410 * the deadline given by repaint_msec? In that case we delay until
2411 * the deadline of the next frame, to give clients a more predictable
2412 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002413 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002414 msec += refresh_nsec / 1000000;
2415
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002416 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002417 output_repaint_timer_handler(output);
2418 else
2419 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002420}
2421
2422static void
2423idle_repaint(void *data)
2424{
2425 struct weston_output *output = data;
2426
Jonas Ådahle5a12252013-04-05 23:07:11 +02002427 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002428}
2429
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002430WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002431weston_layer_entry_insert(struct weston_layer_entry *list,
2432 struct weston_layer_entry *entry)
2433{
2434 wl_list_insert(&list->link, &entry->link);
2435 entry->layer = list->layer;
2436}
2437
2438WL_EXPORT void
2439weston_layer_entry_remove(struct weston_layer_entry *entry)
2440{
2441 wl_list_remove(&entry->link);
2442 wl_list_init(&entry->link);
2443 entry->layer = NULL;
2444}
2445
2446WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002447weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2448{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002449 wl_list_init(&layer->view_list.link);
2450 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002451 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002452 if (below != NULL)
2453 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002454}
2455
2456WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002457weston_layer_set_mask(struct weston_layer *layer,
2458 int x, int y, int width, int height)
2459{
2460 struct weston_view *view;
2461
2462 layer->mask.x1 = x;
2463 layer->mask.x2 = x + width;
2464 layer->mask.y1 = y;
2465 layer->mask.y2 = y + height;
2466
2467 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2468 weston_view_geometry_dirty(view);
2469 }
2470}
2471
2472WL_EXPORT void
2473weston_layer_set_mask_infinite(struct weston_layer *layer)
2474{
2475 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2476 UINT32_MAX, UINT32_MAX);
2477}
2478
2479WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002480weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002481{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002482 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002483 struct wl_event_loop *loop;
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002484 uint32_t inhibited_outputs_mask = weston_compositor_inhibited_outputs(compositor);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002485
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002486 /* If we're offscreen, or if we're sleeping and the monitor
2487 * isn't currently being inhibited by an active surface, then
2488 * skip repainting.
2489 */
2490 if (compositor->state == WESTON_COMPOSITOR_OFFSCREEN ||
2491 (compositor->state == WESTON_COMPOSITOR_SLEEPING &&
2492 !(inhibited_outputs_mask & (1 << output->id))))
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002493 return;
2494
Pekka Paalanenb5026542014-11-12 15:09:24 +02002495 if (!output->repaint_needed)
2496 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2497
Kristian Høgsbergef044142011-06-21 15:02:12 -04002498 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002499 output->repaint_needed = 1;
2500 if (output->repaint_scheduled)
2501 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002502
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002503 wl_event_loop_add_idle(loop, idle_repaint, output);
2504 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002505 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002506}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002507
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002508WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002509weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2510{
2511 struct weston_output *output;
2512
2513 wl_list_for_each(output, &compositor->output_list, link)
2514 weston_output_schedule_repaint(output);
2515}
2516
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002517static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002518surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002519{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002520 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002521}
2522
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002523static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002524surface_attach(struct wl_client *client,
2525 struct wl_resource *resource,
2526 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2527{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002528 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002529 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002530
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002531 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002532 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002533 if (buffer == NULL) {
2534 wl_client_post_no_memory(client);
2535 return;
2536 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002537 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002538
Pekka Paalanende685b82012-12-04 15:58:12 +02002539 /* Attach, attach, without commit in between does not send
2540 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002541 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002542
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002543 surface->pending.sx = sx;
2544 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002545 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002546}
2547
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002548static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002549surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002550 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002551 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002552{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002553 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002554
Derek Foreman57e92ed2015-11-17 14:11:35 -06002555 if (width <= 0 || height <= 0)
2556 return;
2557
Derek Foreman152254b2015-11-26 14:17:48 -06002558 pixman_region32_union_rect(&surface->pending.damage_surface,
2559 &surface->pending.damage_surface,
2560 x, y, width, height);
2561}
2562
2563static void
2564surface_damage_buffer(struct wl_client *client,
2565 struct wl_resource *resource,
2566 int32_t x, int32_t y, int32_t width, int32_t height)
2567{
2568 struct weston_surface *surface = wl_resource_get_user_data(resource);
2569
2570 if (width <= 0 || height <= 0)
2571 return;
2572
2573 pixman_region32_union_rect(&surface->pending.damage_buffer,
2574 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002575 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002576}
2577
Kristian Høgsberg33418202011-08-16 23:01:28 -04002578static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002579destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002580{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002581 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002582
2583 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002584 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002585}
2586
2587static void
2588surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002589 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002590{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002591 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002592 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002593
2594 cb = malloc(sizeof *cb);
2595 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002596 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002597 return;
2598 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002599
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002600 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2601 callback);
2602 if (cb->resource == NULL) {
2603 free(cb);
2604 wl_resource_post_no_memory(resource);
2605 return;
2606 }
2607
Jason Ekstranda85118c2013-06-27 20:17:02 -05002608 wl_resource_set_implementation(cb->resource, NULL, cb,
2609 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002610
Pekka Paalanenbc106382012-10-10 12:49:31 +03002611 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002612}
2613
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002614static void
2615surface_set_opaque_region(struct wl_client *client,
2616 struct wl_resource *resource,
2617 struct wl_resource *region_resource)
2618{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002619 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002620 struct weston_region *region;
2621
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002622 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002623 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002624 pixman_region32_copy(&surface->pending.opaque,
2625 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002626 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002627 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002628 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002629}
2630
2631static void
2632surface_set_input_region(struct wl_client *client,
2633 struct wl_resource *resource,
2634 struct wl_resource *region_resource)
2635{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002636 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002637 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002638
2639 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002640 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002641 pixman_region32_copy(&surface->pending.input,
2642 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002643 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002644 pixman_region32_fini(&surface->pending.input);
2645 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002646 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002647}
2648
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002649static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002650weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002651{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002652 struct weston_subsurface *sub;
2653
2654 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2655 parent_link_pending) {
2656 wl_list_remove(&sub->parent_link);
2657 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2658 }
2659}
2660
2661static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002662weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002663 struct weston_matrix *matrix)
2664{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002665 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002666 double src_width, src_height, dest_width, dest_height;
2667
2668 weston_matrix_init(matrix);
2669
2670 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2671 src_width = surface->width_from_buffer;
2672 src_height = surface->height_from_buffer;
2673 } else {
2674 src_width = wl_fixed_to_double(vp->buffer.src_width);
2675 src_height = wl_fixed_to_double(vp->buffer.src_height);
2676 }
2677
2678 if (vp->surface.width == -1) {
2679 dest_width = src_width;
2680 dest_height = src_height;
2681 } else {
2682 dest_width = vp->surface.width;
2683 dest_height = vp->surface.height;
2684 }
2685
2686 if (src_width != dest_width || src_height != dest_height)
2687 weston_matrix_scale(matrix,
2688 src_width / dest_width,
2689 src_height / dest_height, 1);
2690
2691 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2692 weston_matrix_translate(matrix,
2693 wl_fixed_to_double(vp->buffer.src_x),
2694 wl_fixed_to_double(vp->buffer.src_y),
2695 0);
2696
2697 switch (vp->buffer.transform) {
2698 case WL_OUTPUT_TRANSFORM_FLIPPED:
2699 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2700 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2701 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2702 weston_matrix_scale(matrix, -1, 1, 1);
2703 weston_matrix_translate(matrix,
2704 surface->width_from_buffer, 0, 0);
2705 break;
2706 }
2707
2708 switch (vp->buffer.transform) {
2709 default:
2710 case WL_OUTPUT_TRANSFORM_NORMAL:
2711 case WL_OUTPUT_TRANSFORM_FLIPPED:
2712 break;
2713 case WL_OUTPUT_TRANSFORM_90:
2714 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2715 weston_matrix_rotate_xy(matrix, 0, 1);
2716 weston_matrix_translate(matrix,
2717 surface->height_from_buffer, 0, 0);
2718 break;
2719 case WL_OUTPUT_TRANSFORM_180:
2720 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2721 weston_matrix_rotate_xy(matrix, -1, 0);
2722 weston_matrix_translate(matrix,
2723 surface->width_from_buffer,
2724 surface->height_from_buffer, 0);
2725 break;
2726 case WL_OUTPUT_TRANSFORM_270:
2727 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2728 weston_matrix_rotate_xy(matrix, 0, -1);
2729 weston_matrix_translate(matrix,
2730 0, surface->width_from_buffer, 0);
2731 break;
2732 }
2733
2734 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2735}
2736
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002737/**
2738 * Compute a + b > c while being safe to overflows.
2739 */
2740static bool
2741fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2742{
2743 return (int64_t)a + (int64_t)b > (int64_t)c;
2744}
2745
2746static bool
2747weston_surface_is_pending_viewport_source_valid(
2748 const struct weston_surface *surface)
2749{
2750 const struct weston_surface_state *pend = &surface->pending;
2751 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2752 int width_from_buffer = 0;
2753 int height_from_buffer = 0;
2754 wl_fixed_t w;
2755 wl_fixed_t h;
2756
2757 /* If viewport source rect is not set, it is always ok. */
2758 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2759 return true;
2760
2761 if (pend->newly_attached) {
2762 if (pend->buffer) {
2763 convert_size_by_transform_scale(&width_from_buffer,
2764 &height_from_buffer,
2765 pend->buffer->width,
2766 pend->buffer->height,
2767 vp->buffer.transform,
2768 vp->buffer.scale);
2769 } else {
2770 /* No buffer: viewport is irrelevant. */
2771 return true;
2772 }
2773 } else {
2774 width_from_buffer = surface->width_from_buffer;
2775 height_from_buffer = surface->height_from_buffer;
2776 }
2777
2778 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2779 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2780
2781 /* No buffer: viewport is irrelevant. */
2782 if (width_from_buffer == 0 || height_from_buffer == 0)
2783 return true;
2784
2785 /* overflow checks for wl_fixed_from_int() */
2786 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2787 return false;
2788 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2789 return false;
2790
2791 w = wl_fixed_from_int(width_from_buffer);
2792 h = wl_fixed_from_int(height_from_buffer);
2793
2794 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2795 return false;
2796 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2797 return false;
2798
2799 return true;
2800}
2801
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002802static bool
2803fixed_is_integer(wl_fixed_t v)
2804{
2805 return (v & 0xff) == 0;
2806}
2807
2808static bool
2809weston_surface_is_pending_viewport_dst_size_int(
2810 const struct weston_surface *surface)
2811{
2812 const struct weston_buffer_viewport *vp =
2813 &surface->pending.buffer_viewport;
2814
2815 if (vp->surface.width != -1) {
2816 assert(vp->surface.width > 0 && vp->surface.height > 0);
2817 return true;
2818 }
2819
2820 return fixed_is_integer(vp->buffer.src_width) &&
2821 fixed_is_integer(vp->buffer.src_height);
2822}
2823
Derek Foreman152254b2015-11-26 14:17:48 -06002824/* Translate pending damage in buffer co-ordinates to surface
2825 * co-ordinates and union it with a pixman_region32_t.
2826 * This should only be called after the buffer is attached.
2827 */
2828static void
2829apply_damage_buffer(pixman_region32_t *dest,
2830 struct weston_surface *surface,
2831 struct weston_surface_state *state)
2832{
2833 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2834
2835 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2836 * translated into surface co-ordinates and unioned with
2837 * any other surface damage.
2838 * None of this makes sense if there is no buffer though.
2839 */
2840 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2841 pixman_region32_t buffer_damage;
2842
2843 pixman_region32_intersect_rect(&state->damage_buffer,
2844 &state->damage_buffer,
2845 0, 0, buffer->width,
2846 buffer->height);
2847 pixman_region32_init(&buffer_damage);
2848 weston_matrix_transform_region(&buffer_damage,
2849 &surface->buffer_to_surface_matrix,
2850 &state->damage_buffer);
2851 pixman_region32_union(dest, dest, &buffer_damage);
2852 pixman_region32_fini(&buffer_damage);
2853 }
2854 /* We should clear this on commit even if there was no buffer */
2855 pixman_region32_clear(&state->damage_buffer);
2856}
2857
Jason Ekstrand1e059042014-10-16 10:55:19 -05002858static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002859weston_surface_commit_state(struct weston_surface *surface,
2860 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002861{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002862 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002863 pixman_region32_t opaque;
2864
Alexander Larsson4ea95522013-05-22 14:41:37 +02002865 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002866 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002867 /* wp_viewport.set_source */
2868 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002869 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002870
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002871 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002872 if (state->newly_attached)
2873 weston_surface_attach(surface, state->buffer);
2874 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002875
Jason Ekstrand1e059042014-10-16 10:55:19 -05002876 weston_surface_build_buffer_matrix(surface,
2877 &surface->surface_to_buffer_matrix);
2878 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2879 &surface->surface_to_buffer_matrix);
2880
Jason Ekstrand7b982072014-05-20 14:33:03 -05002881 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002882 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002883 if (surface->committed)
2884 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002885 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002886
Jason Ekstrand7b982072014-05-20 14:33:03 -05002887 state->sx = 0;
2888 state->sy = 0;
2889 state->newly_attached = 0;
2890 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002891
Derek Foreman152254b2015-11-26 14:17:48 -06002892 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002893 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002894 (pixman_region32_not_empty(&state->damage_surface) ||
2895 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002896 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002897
Pekka Paalanen8e159182012-10-10 12:49:25 +03002898 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002899 &state->damage_surface);
2900
2901 apply_damage_buffer(&surface->damage, surface, state);
2902
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002903 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002904 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002905 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002906
2907 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002908 pixman_region32_init(&opaque);
2909 pixman_region32_intersect_rect(&opaque, &state->opaque,
2910 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002911
2912 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2913 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002914 wl_list_for_each(view, &surface->views, surface_link)
2915 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002916 }
2917
2918 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002919
2920 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002921 pixman_region32_intersect_rect(&surface->input, &state->input,
2922 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002923
Pekka Paalanenbc106382012-10-10 12:49:31 +03002924 /* wl_surface.frame */
2925 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002926 &state->frame_callback_list);
2927 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002928
2929 /* XXX:
2930 * What should happen with a feedback request, if there
2931 * is no wl_buffer attached for this commit?
2932 */
2933
2934 /* presentation.feedback */
2935 wl_list_insert_list(&surface->feedback_list,
2936 &state->feedback_list);
2937 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08002938
2939 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002940}
2941
2942static void
2943weston_surface_commit(struct weston_surface *surface)
2944{
2945 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002946
Pekka Paalanene67858b2013-04-25 13:57:42 +03002947 weston_surface_commit_subsurface_order(surface);
2948
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002949 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002950}
2951
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002952static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002953weston_subsurface_commit(struct weston_subsurface *sub);
2954
2955static void
2956weston_subsurface_parent_commit(struct weston_subsurface *sub,
2957 int parent_is_synchronized);
2958
2959static void
2960surface_commit(struct wl_client *client, struct wl_resource *resource)
2961{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002962 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002963 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2964
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002965 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2966 assert(surface->viewport_resource);
2967
2968 wl_resource_post_error(surface->viewport_resource,
2969 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2970 "wl_surface@%d has viewport source outside buffer",
2971 wl_resource_get_id(resource));
2972 return;
2973 }
2974
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002975 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
2976 assert(surface->viewport_resource);
2977
2978 wl_resource_post_error(surface->viewport_resource,
2979 WP_VIEWPORT_ERROR_BAD_SIZE,
2980 "wl_surface@%d viewport dst size not integer",
2981 wl_resource_get_id(resource));
2982 return;
2983 }
2984
Pekka Paalanene67858b2013-04-25 13:57:42 +03002985 if (sub) {
2986 weston_subsurface_commit(sub);
2987 return;
2988 }
2989
2990 weston_surface_commit(surface);
2991
2992 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2993 if (sub->surface != surface)
2994 weston_subsurface_parent_commit(sub, 0);
2995 }
2996}
2997
2998static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002999surface_set_buffer_transform(struct wl_client *client,
3000 struct wl_resource *resource, int transform)
3001{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003002 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003003
Jonny Lamba55f1392014-05-30 12:07:15 +02003004 /* if wl_output.transform grows more members this will need to be updated. */
3005 if (transform < 0 ||
3006 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3007 wl_resource_post_error(resource,
3008 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3009 "buffer transform must be a valid transform "
3010 "('%d' specified)", transform);
3011 return;
3012 }
3013
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003014 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003015 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003016}
3017
Alexander Larsson4ea95522013-05-22 14:41:37 +02003018static void
3019surface_set_buffer_scale(struct wl_client *client,
3020 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003021 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003022{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003023 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003024
Jonny Lamba55f1392014-05-30 12:07:15 +02003025 if (scale < 1) {
3026 wl_resource_post_error(resource,
3027 WL_SURFACE_ERROR_INVALID_SCALE,
3028 "buffer scale must be at least one "
3029 "('%d' specified)", scale);
3030 return;
3031 }
3032
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003033 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003034 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003035}
3036
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003037static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003038 surface_destroy,
3039 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003040 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003041 surface_frame,
3042 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003043 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003044 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003045 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003046 surface_set_buffer_scale,
3047 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003048};
3049
3050static void
3051compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003052 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003053{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003054 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003055 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003056
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003057 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003058 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003059 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003060 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003061 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003062
Jason Ekstranda85118c2013-06-27 20:17:02 -05003063 surface->resource =
3064 wl_resource_create(client, &wl_surface_interface,
3065 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003066 if (surface->resource == NULL) {
3067 weston_surface_destroy(surface);
3068 wl_resource_post_no_memory(resource);
3069 return;
3070 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003071 wl_resource_set_implementation(surface->resource, &surface_interface,
3072 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003073
3074 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003075}
3076
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003077static void
3078destroy_region(struct wl_resource *resource)
3079{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003080 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003081
3082 pixman_region32_fini(&region->region);
3083 free(region);
3084}
3085
3086static void
3087region_destroy(struct wl_client *client, struct wl_resource *resource)
3088{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003089 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003090}
3091
3092static void
3093region_add(struct wl_client *client, struct wl_resource *resource,
3094 int32_t x, int32_t y, int32_t width, int32_t height)
3095{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003096 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003097
3098 pixman_region32_union_rect(&region->region, &region->region,
3099 x, y, width, height);
3100}
3101
3102static void
3103region_subtract(struct wl_client *client, struct wl_resource *resource,
3104 int32_t x, int32_t y, int32_t width, int32_t height)
3105{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003106 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003107 pixman_region32_t rect;
3108
3109 pixman_region32_init_rect(&rect, x, y, width, height);
3110 pixman_region32_subtract(&region->region, &region->region, &rect);
3111 pixman_region32_fini(&rect);
3112}
3113
3114static const struct wl_region_interface region_interface = {
3115 region_destroy,
3116 region_add,
3117 region_subtract
3118};
3119
3120static void
3121compositor_create_region(struct wl_client *client,
3122 struct wl_resource *resource, uint32_t id)
3123{
3124 struct weston_region *region;
3125
3126 region = malloc(sizeof *region);
3127 if (region == NULL) {
3128 wl_resource_post_no_memory(resource);
3129 return;
3130 }
3131
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003132 pixman_region32_init(&region->region);
3133
Jason Ekstranda85118c2013-06-27 20:17:02 -05003134 region->resource =
3135 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003136 if (region->resource == NULL) {
3137 free(region);
3138 wl_resource_post_no_memory(resource);
3139 return;
3140 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003141 wl_resource_set_implementation(region->resource, &region_interface,
3142 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003143}
3144
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003145static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003146 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003147 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003148};
3149
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003150static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003151weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3152{
3153 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003154
Jason Ekstrand7b982072014-05-20 14:33:03 -05003155 weston_surface_commit_state(surface, &sub->cached);
3156 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003157
3158 weston_surface_commit_subsurface_order(surface);
3159
3160 weston_surface_schedule_repaint(surface);
3161
Jason Ekstrand7b982072014-05-20 14:33:03 -05003162 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003163}
3164
3165static void
3166weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3167{
3168 struct weston_surface *surface = sub->surface;
3169
3170 /*
3171 * If this commit would cause the surface to move by the
3172 * attach(dx, dy) parameters, the old damage region must be
3173 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003174 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003175 */
Derek Foreman152254b2015-11-26 14:17:48 -06003176 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003177 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003178 pixman_region32_union(&sub->cached.damage_surface,
3179 &sub->cached.damage_surface,
3180 &surface->pending.damage_surface);
3181 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003182
3183 if (surface->pending.newly_attached) {
3184 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003185 weston_surface_state_set_buffer(&sub->cached,
3186 surface->pending.buffer);
3187 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003188 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003189 weston_presentation_feedback_discard_list(
3190 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003191 }
3192 sub->cached.sx += surface->pending.sx;
3193 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003194
Derek Foreman152254b2015-11-26 14:17:48 -06003195 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3196
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003197 sub->cached.buffer_viewport.changed |=
3198 surface->pending.buffer_viewport.changed;
3199 sub->cached.buffer_viewport.buffer =
3200 surface->pending.buffer_viewport.buffer;
3201 sub->cached.buffer_viewport.surface =
3202 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003203
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003204 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003205
3206 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3207
3208 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3209
3210 wl_list_insert_list(&sub->cached.frame_callback_list,
3211 &surface->pending.frame_callback_list);
3212 wl_list_init(&surface->pending.frame_callback_list);
3213
Pekka Paalanen133e4392014-09-23 22:08:46 -04003214 wl_list_insert_list(&sub->cached.feedback_list,
3215 &surface->pending.feedback_list);
3216 wl_list_init(&surface->pending.feedback_list);
3217
Jason Ekstrand7b982072014-05-20 14:33:03 -05003218 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003219}
3220
Derek Foreman280e7dd2014-10-03 13:13:42 -05003221static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003222weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3223{
3224 while (sub) {
3225 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003226 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003227
3228 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003229 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003230
3231 sub = weston_surface_to_subsurface(sub->parent);
3232 }
3233
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003234 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003235}
3236
3237static void
3238weston_subsurface_commit(struct weston_subsurface *sub)
3239{
3240 struct weston_surface *surface = sub->surface;
3241 struct weston_subsurface *tmp;
3242
3243 /* Recursive check for effectively synchronized. */
3244 if (weston_subsurface_is_synchronized(sub)) {
3245 weston_subsurface_commit_to_cache(sub);
3246 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003247 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003248 /* flush accumulated state from cache */
3249 weston_subsurface_commit_to_cache(sub);
3250 weston_subsurface_commit_from_cache(sub);
3251 } else {
3252 weston_surface_commit(surface);
3253 }
3254
3255 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3256 if (tmp->surface != surface)
3257 weston_subsurface_parent_commit(tmp, 0);
3258 }
3259 }
3260}
3261
3262static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003263weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003264{
3265 struct weston_surface *surface = sub->surface;
3266 struct weston_subsurface *tmp;
3267
Pekka Paalanene67858b2013-04-25 13:57:42 +03003268 /* From now on, commit_from_cache the whole sub-tree, regardless of
3269 * the synchronized mode of each child. This sub-surface or some
3270 * of its ancestors were synchronized, so we are synchronized
3271 * all the way down.
3272 */
3273
Jason Ekstrand7b982072014-05-20 14:33:03 -05003274 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003275 weston_subsurface_commit_from_cache(sub);
3276
3277 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3278 if (tmp->surface != surface)
3279 weston_subsurface_parent_commit(tmp, 1);
3280 }
3281}
3282
3283static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003284weston_subsurface_parent_commit(struct weston_subsurface *sub,
3285 int parent_is_synchronized)
3286{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003287 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003288 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003289 wl_list_for_each(view, &sub->surface->views, surface_link)
3290 weston_view_set_position(view,
3291 sub->position.x,
3292 sub->position.y);
3293
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003294 sub->position.set = 0;
3295 }
3296
3297 if (parent_is_synchronized || sub->synchronized)
3298 weston_subsurface_synchronized_commit(sub);
3299}
3300
Pekka Paalanen8274d902014-08-06 19:36:51 +03003301static int
3302subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3303{
3304 return snprintf(buf, len, "sub-surface");
3305}
3306
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003307static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003308subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003309{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003310 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003311
Jason Ekstranda7af7042013-10-12 22:38:11 -05003312 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003313 weston_view_set_position(view,
3314 view->geometry.x + dx,
3315 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003316
3317 /* No need to check parent mappedness, because if parent is not
3318 * mapped, parent is not in a visible layer, so this sub-surface
3319 * will not be drawn either.
3320 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003321
Pekka Paalanene67858b2013-04-25 13:57:42 +03003322 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003323 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003324
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003325 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003326 * because that would call it also for the parent surface,
3327 * which might not be mapped yet. That would lead to
3328 * inconsistent state, where the window could never be
3329 * mapped.
3330 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003331 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003332 * weston_surface_is_mapped() return true, so that when the
3333 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003334 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003335 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003336 }
3337}
3338
3339static struct weston_subsurface *
3340weston_surface_to_subsurface(struct weston_surface *surface)
3341{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003342 if (surface->committed == subsurface_committed)
3343 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003344
3345 return NULL;
3346}
3347
Pekka Paalanen01388e22013-04-25 13:57:44 +03003348WL_EXPORT struct weston_surface *
3349weston_surface_get_main_surface(struct weston_surface *surface)
3350{
3351 struct weston_subsurface *sub;
3352
3353 while (surface && (sub = weston_surface_to_subsurface(surface)))
3354 surface = sub->parent;
3355
3356 return surface;
3357}
3358
Pekka Paalanen50b67472014-10-01 15:02:41 +03003359WL_EXPORT int
3360weston_surface_set_role(struct weston_surface *surface,
3361 const char *role_name,
3362 struct wl_resource *error_resource,
3363 uint32_t error_code)
3364{
3365 assert(role_name);
3366
3367 if (surface->role_name == NULL ||
3368 surface->role_name == role_name ||
3369 strcmp(surface->role_name, role_name) == 0) {
3370 surface->role_name = role_name;
3371
3372 return 0;
3373 }
3374
3375 wl_resource_post_error(error_resource, error_code,
3376 "Cannot assign role %s to wl_surface@%d,"
3377 " already has role %s\n",
3378 role_name,
3379 wl_resource_get_id(surface->resource),
3380 surface->role_name);
3381 return -1;
3382}
3383
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003384WL_EXPORT const char *
3385weston_surface_get_role(struct weston_surface *surface)
3386{
3387 return surface->role_name;
3388}
3389
Pekka Paalanen8274d902014-08-06 19:36:51 +03003390WL_EXPORT void
3391weston_surface_set_label_func(struct weston_surface *surface,
3392 int (*desc)(struct weston_surface *,
3393 char *, size_t))
3394{
3395 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003396 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003397}
3398
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003399/** Get the size of surface contents
3400 *
3401 * \param surface The surface to query.
3402 * \param width Returns the width of raw contents.
3403 * \param height Returns the height of raw contents.
3404 *
3405 * Retrieves the raw surface content size in pixels for the given surface.
3406 * This is the whole content size in buffer pixels. If the surface
3407 * has no content or the renderer does not implement this feature,
3408 * zeroes are returned.
3409 *
3410 * This function is used to determine the buffer size needed for
3411 * a weston_surface_copy_content() call.
3412 */
3413WL_EXPORT void
3414weston_surface_get_content_size(struct weston_surface *surface,
3415 int *width, int *height)
3416{
3417 struct weston_renderer *rer = surface->compositor->renderer;
3418
3419 if (!rer->surface_get_content_size) {
3420 *width = 0;
3421 *height = 0;
3422 return;
3423 }
3424
3425 rer->surface_get_content_size(surface, width, height);
3426}
3427
Quentin Glidic248dd102016-08-12 10:41:34 +02003428/** Get the bounding box of a surface and its subsurfaces
3429 *
3430 * \param surface The surface to query.
3431 * \return The bounding box relative to the surface origin.
3432 *
3433 */
3434WL_EXPORT struct weston_geometry
3435weston_surface_get_bounding_box(struct weston_surface *surface)
3436{
3437 pixman_region32_t region;
3438 pixman_box32_t *box;
3439 struct weston_subsurface *subsurface;
3440
3441 pixman_region32_init_rect(&region,
3442 0, 0,
3443 surface->width, surface->height);
3444
3445 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3446 pixman_region32_union_rect(&region, &region,
3447 subsurface->position.x,
3448 subsurface->position.y,
3449 subsurface->surface->width,
3450 subsurface->surface->height);
3451
3452 box = pixman_region32_extents(&region);
3453 struct weston_geometry geometry = {
3454 .x = box->x1,
3455 .y = box->y1,
3456 .width = box->x2 - box->x1,
3457 .height = box->y2 - box->y1,
3458 };
3459
3460 pixman_region32_fini(&region);
3461
3462 return geometry;
3463}
3464
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003465/** Copy surface contents to system memory.
3466 *
3467 * \param surface The surface to copy from.
3468 * \param target Pointer to the target memory buffer.
3469 * \param size Size of the target buffer in bytes.
3470 * \param src_x X location on contents to copy from.
3471 * \param src_y Y location on contents to copy from.
3472 * \param width Width in pixels of the area to copy.
3473 * \param height Height in pixels of the area to copy.
3474 * \return 0 for success, -1 for failure.
3475 *
3476 * Surface contents are maintained by the renderer. They can be in a
3477 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3478 * else.
3479 *
3480 * Surface contents are copied into memory pointed to by target,
3481 * which has size bytes of space available. The target memory
3482 * may be larger than needed, but being smaller returns an error.
3483 * The extra bytes in target may or may not be written; their content is
3484 * unspecified. Size must be large enough to hold the image.
3485 *
3486 * The image in the target memory will be arranged in rows from
3487 * top to bottom, and pixels on a row from left to right. The pixel
3488 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3489 * width * 4.
3490 *
3491 * Parameters src_x and src_y define the upper-left corner in buffer
3492 * coordinates (pixels) to copy from. Parameters width and height
3493 * define the size of the area to copy in pixels.
3494 *
3495 * The rectangle defined by src_x, src_y, width, height must fit in
3496 * the surface contents. Otherwise an error is returned.
3497 *
3498 * Use surface_get_data_size to determine the content size; the
3499 * needed target buffer size and rectangle limits.
3500 *
3501 * CURRENT IMPLEMENTATION RESTRICTIONS:
3502 * - the machine must be little-endian due to Pixman formats.
3503 *
3504 * NOTE: Pixman formats are premultiplied.
3505 */
3506WL_EXPORT int
3507weston_surface_copy_content(struct weston_surface *surface,
3508 void *target, size_t size,
3509 int src_x, int src_y,
3510 int width, int height)
3511{
3512 struct weston_renderer *rer = surface->compositor->renderer;
3513 int cw, ch;
3514 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3515
3516 if (!rer->surface_copy_content)
3517 return -1;
3518
3519 weston_surface_get_content_size(surface, &cw, &ch);
3520
3521 if (src_x < 0 || src_y < 0)
3522 return -1;
3523
3524 if (width <= 0 || height <= 0)
3525 return -1;
3526
3527 if (src_x + width > cw || src_y + height > ch)
3528 return -1;
3529
3530 if (width * bytespp * height > size)
3531 return -1;
3532
3533 return rer->surface_copy_content(surface, target, size,
3534 src_x, src_y, width, height);
3535}
3536
Pekka Paalanene67858b2013-04-25 13:57:42 +03003537static void
3538subsurface_set_position(struct wl_client *client,
3539 struct wl_resource *resource, int32_t x, int32_t y)
3540{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003541 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003542
3543 if (!sub)
3544 return;
3545
3546 sub->position.x = x;
3547 sub->position.y = y;
3548 sub->position.set = 1;
3549}
3550
3551static struct weston_subsurface *
3552subsurface_from_surface(struct weston_surface *surface)
3553{
3554 struct weston_subsurface *sub;
3555
3556 sub = weston_surface_to_subsurface(surface);
3557 if (sub)
3558 return sub;
3559
3560 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3561 if (sub->surface == surface)
3562 return sub;
3563
3564 return NULL;
3565}
3566
3567static struct weston_subsurface *
3568subsurface_sibling_check(struct weston_subsurface *sub,
3569 struct weston_surface *surface,
3570 const char *request)
3571{
3572 struct weston_subsurface *sibling;
3573
3574 sibling = subsurface_from_surface(surface);
3575
3576 if (!sibling) {
3577 wl_resource_post_error(sub->resource,
3578 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3579 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003580 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003581 return NULL;
3582 }
3583
3584 if (sibling->parent != sub->parent) {
3585 wl_resource_post_error(sub->resource,
3586 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3587 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003588 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003589 return NULL;
3590 }
3591
3592 return sibling;
3593}
3594
3595static void
3596subsurface_place_above(struct wl_client *client,
3597 struct wl_resource *resource,
3598 struct wl_resource *sibling_resource)
3599{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003600 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003601 struct weston_surface *surface =
3602 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003603 struct weston_subsurface *sibling;
3604
3605 if (!sub)
3606 return;
3607
3608 sibling = subsurface_sibling_check(sub, surface, "place_above");
3609 if (!sibling)
3610 return;
3611
3612 wl_list_remove(&sub->parent_link_pending);
3613 wl_list_insert(sibling->parent_link_pending.prev,
3614 &sub->parent_link_pending);
3615}
3616
3617static void
3618subsurface_place_below(struct wl_client *client,
3619 struct wl_resource *resource,
3620 struct wl_resource *sibling_resource)
3621{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003622 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003623 struct weston_surface *surface =
3624 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003625 struct weston_subsurface *sibling;
3626
3627 if (!sub)
3628 return;
3629
3630 sibling = subsurface_sibling_check(sub, surface, "place_below");
3631 if (!sibling)
3632 return;
3633
3634 wl_list_remove(&sub->parent_link_pending);
3635 wl_list_insert(&sibling->parent_link_pending,
3636 &sub->parent_link_pending);
3637}
3638
3639static void
3640subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3641{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003642 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003643
3644 if (sub)
3645 sub->synchronized = 1;
3646}
3647
3648static void
3649subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3650{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003651 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003652
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003653 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003654 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003655
3656 /* If sub became effectively desynchronized, flush. */
3657 if (!weston_subsurface_is_synchronized(sub))
3658 weston_subsurface_synchronized_commit(sub);
3659 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003660}
3661
3662static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003663weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3664{
3665 wl_list_remove(&sub->parent_link);
3666 wl_list_remove(&sub->parent_link_pending);
3667 wl_list_remove(&sub->parent_destroy_listener.link);
3668 sub->parent = NULL;
3669}
3670
3671static void
3672weston_subsurface_destroy(struct weston_subsurface *sub);
3673
3674static void
3675subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3676{
3677 struct weston_subsurface *sub =
3678 container_of(listener, struct weston_subsurface,
3679 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003680 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003681
3682 /* The protocol object (wl_resource) is left inert. */
3683 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003684 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003685
3686 weston_subsurface_destroy(sub);
3687}
3688
3689static void
3690subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3691{
3692 struct weston_subsurface *sub =
3693 container_of(listener, struct weston_subsurface,
3694 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003695 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003696 assert(sub->surface != sub->parent);
3697
3698 if (weston_surface_is_mapped(sub->surface))
3699 weston_surface_unmap(sub->surface);
3700
3701 weston_subsurface_unlink_parent(sub);
3702}
3703
3704static void
3705subsurface_resource_destroy(struct wl_resource *resource)
3706{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003707 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003708
3709 if (sub)
3710 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003711}
3712
3713static void
3714subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3715{
3716 wl_resource_destroy(resource);
3717}
3718
3719static void
3720weston_subsurface_link_parent(struct weston_subsurface *sub,
3721 struct weston_surface *parent)
3722{
3723 sub->parent = parent;
3724 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003725 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003726 &sub->parent_destroy_listener);
3727
3728 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3729 wl_list_insert(&parent->subsurface_list_pending,
3730 &sub->parent_link_pending);
3731}
3732
3733static void
3734weston_subsurface_link_surface(struct weston_subsurface *sub,
3735 struct weston_surface *surface)
3736{
3737 sub->surface = surface;
3738 sub->surface_destroy_listener.notify =
3739 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003740 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003741 &sub->surface_destroy_listener);
3742}
3743
3744static void
3745weston_subsurface_destroy(struct weston_subsurface *sub)
3746{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003747 struct weston_view *view, *next;
3748
Pekka Paalanene67858b2013-04-25 13:57:42 +03003749 assert(sub->surface);
3750
3751 if (sub->resource) {
3752 assert(weston_surface_to_subsurface(sub->surface) == sub);
3753 assert(sub->parent_destroy_listener.notify ==
3754 subsurface_handle_parent_destroy);
3755
George Kiagiadakised04d382014-06-13 18:10:26 +02003756 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3757 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003758 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003759 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003760
Pekka Paalanene67858b2013-04-25 13:57:42 +03003761 if (sub->parent)
3762 weston_subsurface_unlink_parent(sub);
3763
Jason Ekstrand7b982072014-05-20 14:33:03 -05003764 weston_surface_state_fini(&sub->cached);
3765 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003766
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003767 sub->surface->committed = NULL;
3768 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003769 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003770 } else {
3771 /* the dummy weston_subsurface for the parent itself */
3772 assert(sub->parent_destroy_listener.notify == NULL);
3773 wl_list_remove(&sub->parent_link);
3774 wl_list_remove(&sub->parent_link_pending);
3775 }
3776
3777 wl_list_remove(&sub->surface_destroy_listener.link);
3778 free(sub);
3779}
3780
3781static const struct wl_subsurface_interface subsurface_implementation = {
3782 subsurface_destroy,
3783 subsurface_set_position,
3784 subsurface_place_above,
3785 subsurface_place_below,
3786 subsurface_set_sync,
3787 subsurface_set_desync
3788};
3789
3790static struct weston_subsurface *
3791weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3792 struct weston_surface *parent)
3793{
3794 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003795 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003796
Bryce Harringtonde16d892014-11-20 22:21:57 -08003797 sub = zalloc(sizeof *sub);
3798 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003799 return NULL;
3800
Jason Ekstranda7af7042013-10-12 22:38:11 -05003801 wl_list_init(&sub->unused_views);
3802
Jason Ekstranda85118c2013-06-27 20:17:02 -05003803 sub->resource =
3804 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003805 if (!sub->resource) {
3806 free(sub);
3807 return NULL;
3808 }
3809
Jason Ekstranda85118c2013-06-27 20:17:02 -05003810 wl_resource_set_implementation(sub->resource,
3811 &subsurface_implementation,
3812 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003813 weston_subsurface_link_surface(sub, surface);
3814 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003815 weston_surface_state_init(&sub->cached);
3816 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003817 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003818
3819 return sub;
3820}
3821
3822/* Create a dummy subsurface for having the parent itself in its
3823 * sub-surface lists. Makes stacking order manipulation easy.
3824 */
3825static struct weston_subsurface *
3826weston_subsurface_create_for_parent(struct weston_surface *parent)
3827{
3828 struct weston_subsurface *sub;
3829
Bryce Harringtonde16d892014-11-20 22:21:57 -08003830 sub = zalloc(sizeof *sub);
3831 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003832 return NULL;
3833
3834 weston_subsurface_link_surface(sub, parent);
3835 sub->parent = parent;
3836 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3837 wl_list_insert(&parent->subsurface_list_pending,
3838 &sub->parent_link_pending);
3839
3840 return sub;
3841}
3842
3843static void
3844subcompositor_get_subsurface(struct wl_client *client,
3845 struct wl_resource *resource,
3846 uint32_t id,
3847 struct wl_resource *surface_resource,
3848 struct wl_resource *parent_resource)
3849{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003850 struct weston_surface *surface =
3851 wl_resource_get_user_data(surface_resource);
3852 struct weston_surface *parent =
3853 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003854 struct weston_subsurface *sub;
3855 static const char where[] = "get_subsurface: wl_subsurface@";
3856
3857 if (surface == parent) {
3858 wl_resource_post_error(resource,
3859 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3860 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003861 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003862 return;
3863 }
3864
3865 if (weston_surface_to_subsurface(surface)) {
3866 wl_resource_post_error(resource,
3867 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3868 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003869 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003870 return;
3871 }
3872
Pekka Paalanen50b67472014-10-01 15:02:41 +03003873 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3874 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003875 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003876
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003877 if (weston_surface_get_main_surface(parent) == surface) {
3878 wl_resource_post_error(resource,
3879 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3880 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003881 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003882 return;
3883 }
3884
Pekka Paalanene67858b2013-04-25 13:57:42 +03003885 /* make sure the parent is in its own list */
3886 if (wl_list_empty(&parent->subsurface_list)) {
3887 if (!weston_subsurface_create_for_parent(parent)) {
3888 wl_resource_post_no_memory(resource);
3889 return;
3890 }
3891 }
3892
3893 sub = weston_subsurface_create(id, surface, parent);
3894 if (!sub) {
3895 wl_resource_post_no_memory(resource);
3896 return;
3897 }
3898
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003899 surface->committed = subsurface_committed;
3900 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003901 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003902}
3903
3904static void
3905subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3906{
3907 wl_resource_destroy(resource);
3908}
3909
3910static const struct wl_subcompositor_interface subcompositor_interface = {
3911 subcompositor_destroy,
3912 subcompositor_get_subsurface
3913};
3914
3915static void
3916bind_subcompositor(struct wl_client *client,
3917 void *data, uint32_t version, uint32_t id)
3918{
3919 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003920 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003921
Jason Ekstranda85118c2013-06-27 20:17:02 -05003922 resource =
3923 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003924 if (resource == NULL) {
3925 wl_client_post_no_memory(client);
3926 return;
3927 }
3928 wl_resource_set_implementation(resource, &subcompositor_interface,
3929 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003930}
3931
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003932/** Set a DPMS mode on all of the compositor's outputs
3933 *
3934 * \param compositor The compositor instance
3935 * \param state The DPMS state the outputs will be set to
3936 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003937static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003938weston_compositor_dpms(struct weston_compositor *compositor,
3939 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003940{
3941 struct weston_output *output;
Bryce Harringtonf8300c82016-08-10 17:25:22 -07003942 uint32_t inhibited_outputs_mask = weston_compositor_inhibited_outputs(compositor);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003943
Bryce Harringtonf8300c82016-08-10 17:25:22 -07003944 wl_list_for_each(output, &compositor->output_list, link) {
3945 if (!output->set_dpms)
3946 continue;
3947
3948 /* If output is idle-inhibited, don't toggle to any DPMS state except ON. */
3949 if (state != WESTON_DPMS_ON &&
3950 inhibited_outputs_mask & (1 << output->id))
3951 continue;
3952
3953 output->set_dpms(output, state);
3954 }
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003955}
3956
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003957/** Restores the compositor to active status
3958 *
3959 * \param compositor The compositor instance
3960 *
3961 * If the compositor was in a sleeping mode, all outputs are powered
3962 * back on via DPMS. Otherwise if the compositor was inactive
3963 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3964 * signal will fire.
3965 *
3966 * Restarts the idle timer.
3967 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003968WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003969weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003970{
Neil Roberts8b62e202013-09-30 13:14:47 +01003971 uint32_t old_state = compositor->state;
3972
3973 /* The state needs to be changed before emitting the wake
3974 * signal because that may try to schedule a repaint which
3975 * will not work if the compositor is still sleeping */
3976 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3977
3978 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003979 case WESTON_COMPOSITOR_SLEEPING:
3980 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3981 /* fall through */
3982 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003983 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003984 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003985 /* fall through */
3986 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003987 wl_event_source_timer_update(compositor->idle_source,
3988 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003989 }
3990}
3991
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003992/** Turns off rendering and frame events for the compositor.
3993 *
3994 * \param compositor The compositor instance
3995 *
3996 * This is used for example to prevent further rendering while the
3997 * compositor is shutting down.
3998 *
3999 * \note When offscreen state is entered, outputs will be powered
4000 * back on if they were sleeping (in DPMS off mode), even though
4001 * no rendering will be performed.
4002 *
4003 * Stops the idle timer.
4004 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004005WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004006weston_compositor_offscreen(struct weston_compositor *compositor)
4007{
4008 switch (compositor->state) {
4009 case WESTON_COMPOSITOR_OFFSCREEN:
4010 return;
4011 case WESTON_COMPOSITOR_SLEEPING:
4012 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
4013 /* fall through */
4014 default:
4015 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4016 wl_event_source_timer_update(compositor->idle_source, 0);
4017 }
4018}
4019
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004020/** Powers down all attached output devices
4021 *
4022 * \param compositor The compositor instance
4023 *
4024 * Causes rendering to the outputs to cease, and no frame events to be
4025 * sent. Only powers down the outputs if the compositor is not already
4026 * in sleep mode.
4027 *
4028 * Stops the idle timer.
4029 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004030WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004031weston_compositor_sleep(struct weston_compositor *compositor)
4032{
4033 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4034 return;
4035
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004036 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004037 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4038 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4039}
4040
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004041/** Sets compositor to idle mode
4042 *
4043 * \param data The compositor instance
4044 *
4045 * This is called when the idle timer fires. Once the compositor is in
4046 * idle mode it requires a wake action (e.g. via
4047 * weston_compositor_wake()) to restore it. The compositor's
4048 * idle_signal will be triggered when the idle event occurs.
4049 *
4050 * Idleness can be inhibited by setting the compositor's idle_inhibit
4051 * property.
4052 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004053static int
4054idle_handler(void *data)
4055{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004056 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004057
4058 if (compositor->idle_inhibit)
4059 return 1;
4060
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004061 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004062 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004063
4064 return 1;
4065}
4066
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004067WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004068weston_plane_init(struct weston_plane *plane,
4069 struct weston_compositor *ec,
4070 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004071{
4072 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004073 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004074 plane->x = x;
4075 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004076 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004077
4078 /* Init the link so that the call to wl_list_remove() when releasing
4079 * the plane without ever stacking doesn't lead to a crash */
4080 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004081}
4082
4083WL_EXPORT void
4084weston_plane_release(struct weston_plane *plane)
4085{
Xiong Zhang97116532013-10-23 13:58:31 +08004086 struct weston_view *view;
4087
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004088 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004089 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004090
Xiong Zhang97116532013-10-23 13:58:31 +08004091 wl_list_for_each(view, &plane->compositor->view_list, link) {
4092 if (view->plane == plane)
4093 view->plane = NULL;
4094 }
4095
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004096 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004097}
4098
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004099WL_EXPORT void
4100weston_compositor_stack_plane(struct weston_compositor *ec,
4101 struct weston_plane *plane,
4102 struct weston_plane *above)
4103{
4104 if (above)
4105 wl_list_insert(above->link.prev, &plane->link);
4106 else
4107 wl_list_insert(&ec->plane_list, &plane->link);
4108}
4109
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004110static void
4111output_release(struct wl_client *client, struct wl_resource *resource)
4112{
4113 wl_resource_destroy(resource);
4114}
4115
4116static const struct wl_output_interface output_interface = {
4117 output_release,
4118};
4119
4120
Casey Dahlin9074db52012-04-19 22:50:09 -04004121static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004122{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004123 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004124}
4125
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004126static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004127bind_output(struct wl_client *client,
4128 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004129{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004130 struct weston_output *output = data;
4131 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004132 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004133
Jason Ekstranda85118c2013-06-27 20:17:02 -05004134 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004135 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004136 if (resource == NULL) {
4137 wl_client_post_no_memory(client);
4138 return;
4139 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004140
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004141 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004142 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004143
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004144 wl_output_send_geometry(resource,
4145 output->x,
4146 output->y,
4147 output->mm_width,
4148 output->mm_height,
4149 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004150 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004151 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004152 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004153 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004154 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004155
4156 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004157 wl_output_send_mode(resource,
4158 mode->flags,
4159 mode->width,
4160 mode->height,
4161 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004162 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004163
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004164 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004165 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004166}
4167
David Fort0de859e2016-05-27 23:22:57 +02004168/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004169static void
David Fort0de859e2016-05-27 23:22:57 +02004170weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4171 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004172{
4173 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004174 bool start_resizing = false;
4175
4176 if (!delta_width)
4177 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004178
4179 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004180 if (output == resized_output) {
4181 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004182 continue;
4183 }
4184
David Fort0de859e2016-05-27 23:22:57 +02004185 if (start_resizing) {
4186 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004187 output->dirty = 1;
4188 }
4189 }
4190}
4191
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004192WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004193weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004194{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004195 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004196 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004197
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004198 output->destroying = 1;
4199
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004200 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004201 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004202 weston_view_assign_output(view);
4203 }
4204
Pekka Paalanen0513a952014-05-21 16:17:27 +03004205 wl_event_source_remove(output->repaint_timer);
4206
Pekka Paalanen133e4392014-09-23 22:08:46 -04004207 weston_presentation_feedback_discard_list(&output->feedback_list);
4208
David Fort0de859e2016-05-27 23:22:57 +02004209 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004210 wl_list_remove(&output->link);
4211
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004212 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004213 wl_signal_emit(&output->destroy_signal, output);
4214
Richard Hughesafe690c2013-05-02 10:10:04 +01004215 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004216 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004217 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004218 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004219
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004220 wl_resource_for_each(resource, &output->resource_list) {
4221 wl_resource_set_destructor(resource, NULL);
4222 }
4223
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004224 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004225}
4226
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004227WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004228weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004229{
Scott Moreau850ca422012-05-21 15:21:25 -06004230 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004231
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004232 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004233 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004234
Scott Moreauccbf29d2012-02-22 14:21:41 -07004235 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004236 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004237 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004238 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004239 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004240 weston_matrix_scale(&output->matrix, magnification,
4241 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004242 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004243
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004244 switch (output->transform) {
4245 case WL_OUTPUT_TRANSFORM_FLIPPED:
4246 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4247 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4248 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4249 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4250 weston_matrix_scale(&output->matrix, -1, 1, 1);
4251 break;
4252 }
4253
4254 switch (output->transform) {
4255 default:
4256 case WL_OUTPUT_TRANSFORM_NORMAL:
4257 case WL_OUTPUT_TRANSFORM_FLIPPED:
4258 break;
4259 case WL_OUTPUT_TRANSFORM_90:
4260 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4261 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4262 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4263 break;
4264 case WL_OUTPUT_TRANSFORM_180:
4265 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4266 weston_matrix_translate(&output->matrix,
4267 -output->width, -output->height, 0);
4268 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4269 break;
4270 case WL_OUTPUT_TRANSFORM_270:
4271 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4272 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4273 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4274 break;
4275 }
4276
4277 if (output->current_scale != 1)
4278 weston_matrix_scale(&output->matrix,
4279 output->current_scale,
4280 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004281
Scott Moreauccbf29d2012-02-22 14:21:41 -07004282 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004283
4284 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004285}
4286
Scott Moreau1bad5db2012-08-18 01:04:05 -06004287static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004288weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004289{
4290 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004291 output->native_scale = scale;
4292 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004293
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004294 convert_size_by_transform_scale(&output->width, &output->height,
4295 output->current_mode->width,
4296 output->current_mode->height,
4297 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004298}
4299
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004300static void
4301weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004302{
4303 output->x = x;
4304 output->y = y;
4305
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004306 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004307 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004308 output->width,
4309 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004310}
4311
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004312WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004313weston_output_move(struct weston_output *output, int x, int y)
4314{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004315 struct wl_resource *resource;
4316
4317 output->move_x = x - output->x;
4318 output->move_y = y - output->y;
4319
4320 if (output->move_x == 0 && output->move_y == 0)
4321 return;
4322
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004323 weston_output_init_geometry(output, x, y);
4324
4325 output->dirty = 1;
4326
4327 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004328 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004329
4330 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004331 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004332 wl_output_send_geometry(resource,
4333 output->x,
4334 output->y,
4335 output->mm_width,
4336 output->mm_height,
4337 output->subpixel,
4338 output->make,
4339 output->model,
4340 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004341
FORT David8a120692016-04-26 23:34:06 +02004342 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004343 wl_output_send_done(resource);
4344 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004345}
4346
Bryce Harrington3f650b82015-12-23 11:01:58 -08004347/** Initialize a weston_output object's parameters
4348 *
4349 * \param output The weston_output object to initialize
4350 * \param c The output's compositor
4351 * \param x x coordinate for the output in global coordinate space
4352 * \param y y coordinate for the output in global coordinate space
4353 * \param mm_width Physical width of the output as reported by the backend
4354 * \param mm_height Physical height of the output as reported by the backend
4355 * \param transform Rotation of the output
4356 * \param scale Native scaling factor for the output
4357 *
4358 * Sets up the transformation, zoom, and geometry of the output using
4359 * the input properties.
4360 *
4361 * Establishes a repaint timer for the output with the relevant display
4362 * object's event loop. See output_repaint_timer_handler().
4363 *
4364 * The output is assigned an ID. Weston can support up to 32 distinct
4365 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4366 * is referred to and used to find the first available ID number, and
4367 * then this ID is marked as used in output_id_pool.
4368 *
4369 * The output is also assigned a Wayland global with the wl_output
4370 * external interface.
4371 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004372WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004373weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004374 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004375 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004376{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004377 struct wl_event_loop *loop;
4378
Bryce Harrington18e45732015-12-23 20:53:53 -08004379 /* Verify we haven't reached the limit of 32 available output IDs */
4380 assert(ffs(~c->output_id_pool) > 0);
4381
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004382 output->compositor = c;
4383 output->x = x;
4384 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004385 output->mm_width = mm_width;
4386 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004387 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004388 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004389
Alexander Larsson0b135062013-05-28 16:23:36 +02004390 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004391 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004392
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004393 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004394 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004395
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004396 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004397 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004398 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004399 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004400 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004401 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004402
Pekka Paalanen0513a952014-05-21 16:17:27 +03004403 loop = wl_display_get_event_loop(c->wl_display);
4404 output->repaint_timer = wl_event_loop_add_timer(loop,
4405 output_repaint_timer_handler, output);
4406
Bryce Harrington3f650b82015-12-23 11:01:58 -08004407 /* Invert the output id pool and look for the lowest numbered
4408 * switch (the least significant bit). Take that bit's position
4409 * as our ID, and mark it used in the compositor's output_id_pool.
4410 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004411 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004412 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004413
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004414 output->global =
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004415 wl_global_create(c->wl_display, &wl_output_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004416 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004417}
4418
4419/** Adds an output to the compositor's output list and
4420 * send the compositor's output_created signal.
4421 *
4422 * \param compositor The compositor instance.
4423 * \param output The output to be added.
4424 */
4425WL_EXPORT void
4426weston_compositor_add_output(struct weston_compositor *compositor,
4427 struct weston_output *output)
4428{
Armin Krezoviće5403842016-08-05 15:28:29 +02004429 struct weston_view *view, *next;
4430
Giulio Camuffob1147152015-05-06 21:41:57 +03004431 wl_list_insert(compositor->output_list.prev, &output->link);
4432 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02004433
4434 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4435 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004436}
4437
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004438WL_EXPORT void
4439weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004440 double device_x, double device_y,
4441 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004442{
Derek Foreman0f679412014-10-02 13:41:17 -05004443 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004444 device_x,
4445 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004446 0.0,
4447 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004448
Derek Foreman67a18b92015-03-24 11:36:14 -05004449 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004450
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004451 *x = p.f[0] / p.f[3];
4452 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004453}
4454
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004455static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004456destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004457{
Jonny Lamb74130762013-11-26 18:19:46 +01004458 struct weston_surface *surface =
4459 wl_resource_get_user_data(resource);
4460
Pekka Paalanen4826f872016-04-22 14:14:38 +03004461 if (!surface)
4462 return;
4463
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004464 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004465 surface->pending.buffer_viewport.buffer.src_width =
4466 wl_fixed_from_int(-1);
4467 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004468 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004469}
4470
4471static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004472viewport_destroy(struct wl_client *client,
4473 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004474{
4475 wl_resource_destroy(resource);
4476}
4477
4478static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004479viewport_set_source(struct wl_client *client,
4480 struct wl_resource *resource,
4481 wl_fixed_t src_x,
4482 wl_fixed_t src_y,
4483 wl_fixed_t src_width,
4484 wl_fixed_t src_height)
4485{
4486 struct weston_surface *surface =
4487 wl_resource_get_user_data(resource);
4488
Pekka Paalanen4826f872016-04-22 14:14:38 +03004489 if (!surface) {
4490 wl_resource_post_error(resource,
4491 WP_VIEWPORT_ERROR_NO_SURFACE,
4492 "wl_surface for this viewport is no longer exists");
4493 return;
4494 }
4495
4496 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004497 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004498
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004499 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004500 src_height == wl_fixed_from_int(-1) &&
4501 src_x == wl_fixed_from_int(-1) &&
4502 src_y == wl_fixed_from_int(-1)) {
4503 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004504 surface->pending.buffer_viewport.buffer.src_width =
4505 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004506 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004507 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004508 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004509
Pekka Paalanen201769a2016-04-26 14:42:11 +03004510 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004511 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004512 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004513 "wl_surface@%d viewport source "
4514 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4515 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004516 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004517 wl_fixed_to_double(src_height),
4518 wl_fixed_to_double(src_x),
4519 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004520 return;
4521 }
4522
4523 surface->pending.buffer_viewport.buffer.src_x = src_x;
4524 surface->pending.buffer_viewport.buffer.src_y = src_y;
4525 surface->pending.buffer_viewport.buffer.src_width = src_width;
4526 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004527 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004528}
4529
4530static void
4531viewport_set_destination(struct wl_client *client,
4532 struct wl_resource *resource,
4533 int32_t dst_width,
4534 int32_t dst_height)
4535{
4536 struct weston_surface *surface =
4537 wl_resource_get_user_data(resource);
4538
Pekka Paalanen4826f872016-04-22 14:14:38 +03004539 if (!surface) {
4540 wl_resource_post_error(resource,
4541 WP_VIEWPORT_ERROR_NO_SURFACE,
4542 "wl_surface for this viewport no longer exists");
4543 return;
4544 }
4545
4546 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004547
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004548 if (dst_width == -1 && dst_height == -1) {
4549 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004550 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004551 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004552 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004553 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004554
4555 if (dst_width <= 0 || dst_height <= 0) {
4556 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004557 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004558 "destination size must be positive (%dx%d)",
4559 dst_width, dst_height);
4560 return;
4561 }
4562
4563 surface->pending.buffer_viewport.surface.width = dst_width;
4564 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004565 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004566}
4567
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004568static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004569 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004570 viewport_set_source,
4571 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004572};
4573
4574static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004575viewporter_destroy(struct wl_client *client,
4576 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004577{
4578 wl_resource_destroy(resource);
4579}
4580
4581static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004582viewporter_get_viewport(struct wl_client *client,
4583 struct wl_resource *viewporter,
4584 uint32_t id,
4585 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004586{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004587 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004588 struct weston_surface *surface =
4589 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004590 struct wl_resource *resource;
4591
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004592 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004593 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004594 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004595 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004596 return;
4597 }
4598
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004599 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004600 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004601 if (resource == NULL) {
4602 wl_client_post_no_memory(client);
4603 return;
4604 }
4605
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004606 wl_resource_set_implementation(resource, &viewport_interface,
4607 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004608
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004609 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004610}
4611
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004612static const struct wp_viewporter_interface viewporter_interface = {
4613 viewporter_destroy,
4614 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004615};
4616
4617static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004618bind_viewporter(struct wl_client *client,
4619 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004620{
4621 struct wl_resource *resource;
4622
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004623 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004624 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004625 if (resource == NULL) {
4626 wl_client_post_no_memory(client);
4627 return;
4628 }
4629
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004630 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004631 NULL, NULL);
4632}
4633
4634static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004635destroy_presentation_feedback(struct wl_resource *feedback_resource)
4636{
4637 struct weston_presentation_feedback *feedback;
4638
4639 feedback = wl_resource_get_user_data(feedback_resource);
4640
4641 wl_list_remove(&feedback->link);
4642 free(feedback);
4643}
4644
4645static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004646presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4647{
4648 wl_resource_destroy(resource);
4649}
4650
4651static void
4652presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004653 struct wl_resource *presentation_resource,
4654 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004655 uint32_t callback)
4656{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004657 struct weston_surface *surface;
4658 struct weston_presentation_feedback *feedback;
4659
4660 surface = wl_resource_get_user_data(surface_resource);
4661
Bryce Harringtonde16d892014-11-20 22:21:57 -08004662 feedback = zalloc(sizeof *feedback);
4663 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004664 goto err_calloc;
4665
4666 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004667 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004668 1, callback);
4669 if (!feedback->resource)
4670 goto err_create;
4671
4672 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4673 destroy_presentation_feedback);
4674
4675 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4676
4677 return;
4678
4679err_create:
4680 free(feedback);
4681
4682err_calloc:
4683 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004684}
4685
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004686static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004687 presentation_destroy,
4688 presentation_feedback
4689};
4690
4691static void
4692bind_presentation(struct wl_client *client,
4693 void *data, uint32_t version, uint32_t id)
4694{
4695 struct weston_compositor *compositor = data;
4696 struct wl_resource *resource;
4697
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004698 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004699 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004700 if (resource == NULL) {
4701 wl_client_post_no_memory(client);
4702 return;
4703 }
4704
4705 wl_resource_set_implementation(resource, &presentation_implementation,
4706 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004707 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004708}
4709
4710static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004711compositor_bind(struct wl_client *client,
4712 void *data, uint32_t version, uint32_t id)
4713{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004714 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004715 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004716
Jason Ekstranda85118c2013-06-27 20:17:02 -05004717 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004718 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004719 if (resource == NULL) {
4720 wl_client_post_no_memory(client);
4721 return;
4722 }
4723
4724 wl_resource_set_implementation(resource, &compositor_interface,
4725 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004726}
4727
Bryce Harrington689fece2016-08-10 17:25:23 -07004728struct weston_idle_inhibitor {
4729 struct weston_surface *surface;
4730};
4731
4732static void
4733destroy_idle_inhibitor(struct wl_resource *resource)
4734{
4735 struct weston_idle_inhibitor *inhibitor = wl_resource_get_user_data(resource);
4736
4737 inhibitor->surface = NULL;
4738 free(inhibitor);
4739}
4740
4741static void
4742idle_inhibitor_destroy(struct wl_client *client, struct wl_resource *resource)
4743{
4744 struct weston_idle_inhibitor *inhibitor = wl_resource_get_user_data(resource);
4745
4746 assert(inhibitor);
4747
4748 inhibitor->surface->inhibit_idling = false;
4749}
4750
4751static const struct zwp_idle_inhibitor_v1_interface idle_inhibitor_interface = {
4752 idle_inhibitor_destroy
4753};
4754
4755static void
4756idle_inhibit_manager_destroy(struct wl_client *client, struct wl_resource *resource)
4757{
4758}
4759
4760static void
4761idle_inhibit_manager_create_inhibitor(struct wl_client *client, struct wl_resource *resource,
4762 uint32_t id, struct wl_resource *surface_resource)
4763{
4764 struct weston_surface *surface = wl_resource_get_user_data(surface_resource);
4765 struct weston_idle_inhibitor *inhibitor;
4766 struct wl_resource *cr;
4767
4768 cr = wl_resource_create(client, &zwp_idle_inhibitor_v1_interface,
4769 wl_resource_get_version(resource), id);
4770 if (cr == NULL) {
4771 wl_client_post_no_memory(client);
4772 return;
4773 }
4774
4775 inhibitor = zalloc(sizeof *inhibitor);
4776 if (inhibitor == NULL) {
4777 wl_client_post_no_memory(client);
4778 return;
4779 }
4780
4781 inhibitor->surface = surface;
4782 inhibitor->surface->inhibit_idling = true;
4783
4784 wl_resource_set_implementation(cr, &idle_inhibitor_interface,
4785 inhibitor, destroy_idle_inhibitor);
4786}
4787
4788static const struct zwp_idle_inhibit_manager_v1_interface idle_inhibit_manager_interface = {
4789 idle_inhibit_manager_destroy,
4790 idle_inhibit_manager_create_inhibitor
4791};
4792
4793static void
4794bind_idle_inhibit_manager(struct wl_client *client,
4795 void *data, uint32_t version, uint32_t id)
4796{
4797 struct wl_resource *resource;
4798
4799 resource = wl_resource_create(client, &zwp_idle_inhibit_manager_v1_interface,
4800 version, id);
4801 if (resource == NULL) {
4802 wl_client_post_no_memory(client);
4803 return;
4804 }
4805
4806 wl_resource_set_implementation(resource, &idle_inhibit_manager_interface,
4807 NULL, NULL);
4808}
4809
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004810WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004811weston_environment_get_fd(const char *env)
4812{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07004813 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004814 int fd, flags;
4815
4816 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07004817 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004818 return -1;
4819
4820 flags = fcntl(fd, F_GETFD);
4821 if (flags == -1)
4822 return -1;
4823
4824 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4825 unsetenv(env);
4826
4827 return fd;
4828}
4829
Pekka Paalanenb5026542014-11-12 15:09:24 +02004830static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004831timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004832 uint32_t key, void *data)
4833{
4834 struct weston_compositor *compositor = data;
4835
4836 if (weston_timeline_enabled_)
4837 weston_timeline_close();
4838 else
4839 weston_timeline_open(compositor);
4840}
4841
Giulio Camuffo459137b2014-10-11 23:56:24 +03004842/** Create the compositor.
4843 *
4844 * This functions creates and initializes a compositor instance.
4845 *
4846 * \param display The Wayland display to be used.
4847 * \param user_data A pointer to an object that can later be retrieved
4848 * using the \ref weston_compositor_get_user_data function.
4849 * \return The compositor instance on success or NULL on failure.
4850 */
4851WL_EXPORT struct weston_compositor *
4852weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004853{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004854 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004855 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004856
Giulio Camuffo459137b2014-10-11 23:56:24 +03004857 ec = zalloc(sizeof *ec);
4858 if (!ec)
4859 return NULL;
4860
4861 ec->wl_display = display;
4862 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004863 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004864 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004865 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004866 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004867 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004868 wl_signal_init(&ec->idle_signal);
4869 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004870 wl_signal_init(&ec->show_input_panel_signal);
4871 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004872 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004873 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004874 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004875 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004876 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004877 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004878 wl_signal_init(&ec->session_signal);
4879 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004880
Casey Dahlin58ba1372012-04-19 22:50:08 -04004881 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004882 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004883
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02004884 ec->activate_serial = 1;
4885
Derek Foreman152254b2015-11-26 14:17:48 -06004886 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004887 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004888 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004889
Giulio Camuffo954f1832014-10-11 18:27:30 +03004890 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004891 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004892 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004893
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004894 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004895 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004896 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004897
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004898 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004899 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004900 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004901
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004902 if (weston_input_init(ec) != 0)
4903 goto fail;
4904
Bryce Harrington689fece2016-08-10 17:25:23 -07004905 if (!wl_global_create(ec->wl_display, &zwp_idle_inhibit_manager_v1_interface, 1,
4906 ec, bind_idle_inhibit_manager))
4907 goto fail;
4908
Jason Ekstranda7af7042013-10-12 22:38:11 -05004909 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004910 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004911 wl_list_init(&ec->layer_list);
4912 wl_list_init(&ec->seat_list);
4913 wl_list_init(&ec->output_list);
4914 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004915 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004916 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004917 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004918 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004919 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004920
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004921 wl_list_init(&ec->plugin_api_list);
4922
Xiong Zhang97116532013-10-23 13:58:31 +08004923 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004924 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004925
Giulio Camuffo459137b2014-10-11 23:56:24 +03004926 wl_data_device_manager_init(ec->wl_display);
4927
4928 wl_display_init_shm(ec->wl_display);
4929
4930 loop = wl_display_get_event_loop(ec->wl_display);
4931 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004932
Giulio Camuffo459137b2014-10-11 23:56:24 +03004933 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4934 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4935
4936 weston_compositor_add_debug_binding(ec, KEY_T,
4937 timeline_key_binding_handler, ec);
4938
Giulio Camuffo459137b2014-10-11 23:56:24 +03004939 return ec;
4940
4941fail:
4942 free(ec);
4943 return NULL;
4944}
4945
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004946WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004947weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004948{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004949 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004950
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004951 wl_event_source_remove(ec->idle_source);
4952
Matt Roper361d2ad2011-08-29 13:52:23 -07004953 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004954 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004955 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004956
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004957 if (ec->renderer)
4958 ec->renderer->destroy(ec);
4959
Daniel Stone325fc2d2012-05-30 16:31:58 +01004960 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004961 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004962 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004963 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004964 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004965 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004966
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004967 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004968}
4969
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004970WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004971weston_compositor_exit_with_code(struct weston_compositor *compositor,
4972 int exit_code)
4973{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004974 if (compositor->exit_code == EXIT_SUCCESS)
4975 compositor->exit_code = exit_code;
4976
Giulio Camuffo459137b2014-10-11 23:56:24 +03004977 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004978}
4979
4980WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004981weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4982 const struct weston_pointer_grab_interface *interface)
4983{
4984 struct weston_seat *seat;
4985
4986 ec->default_pointer_grab = interface;
4987 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004988 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4989
4990 if (pointer)
4991 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004992 }
4993}
4994
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004995WL_EXPORT int
4996weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4997 clockid_t clk_id)
4998{
4999 struct timespec ts;
5000
5001 if (clock_gettime(clk_id, &ts) < 0)
5002 return -1;
5003
5004 compositor->presentation_clock = clk_id;
5005
5006 return 0;
5007}
5008
5009/*
5010 * For choosing the software clock, when the display hardware or API
5011 * does not expose a compatible presentation timestamp.
5012 */
5013WL_EXPORT int
5014weston_compositor_set_presentation_clock_software(
5015 struct weston_compositor *compositor)
5016{
5017 /* In order of preference */
5018 static const clockid_t clocks[] = {
5019 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
5020 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
5021 CLOCK_MONOTONIC, /* no jumps, may crawl */
5022 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
5023 CLOCK_REALTIME /* may jump and crawl */
5024 };
5025 unsigned i;
5026
5027 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
5028 if (weston_compositor_set_presentation_clock(compositor,
5029 clocks[i]) == 0)
5030 return 0;
5031
5032 weston_log("Error: no suitable presentation clock available.\n");
5033
5034 return -1;
5035}
5036
Pekka Paalanen662f3842015-03-18 12:17:26 +02005037/** Read the current time from the Presentation clock
5038 *
5039 * \param compositor
5040 * \param ts[out] The current time.
5041 *
5042 * \note Reading the current time in user space is always imprecise to some
5043 * degree.
5044 *
5045 * This function is never meant to fail. If reading the clock does fail,
5046 * an error message is logged and a zero time is returned. Callers are not
5047 * supposed to detect or react to failures.
5048 */
5049WL_EXPORT void
5050weston_compositor_read_presentation_clock(
5051 const struct weston_compositor *compositor,
5052 struct timespec *ts)
5053{
5054 static bool warned;
5055 int ret;
5056
5057 ret = clock_gettime(compositor->presentation_clock, ts);
5058 if (ret < 0) {
5059 ts->tv_sec = 0;
5060 ts->tv_nsec = 0;
5061
5062 if (!warned)
5063 weston_log("Error: failure to read "
5064 "the presentation clock %#x: '%m' (%d)\n",
5065 compositor->presentation_clock, errno);
5066 warned = true;
5067 }
5068}
5069
Pekka Paalanen230f3b12014-09-29 14:18:40 -04005070/** Import dmabuf buffer into current renderer
5071 *
5072 * \param compositor
5073 * \param buffer the dmabuf buffer to import
5074 * \return true on usable buffers, false otherwise
5075 *
5076 * This function tests that the linux_dmabuf_buffer is usable
5077 * for the current renderer. Returns false on unusable buffers. Usually
5078 * usability is tested by importing the dmabufs for composition.
5079 *
5080 * This hook is also used for detecting if the renderer supports
5081 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
5082 * supported.
5083 * */
5084WL_EXPORT bool
5085weston_compositor_import_dmabuf(struct weston_compositor *compositor,
5086 struct linux_dmabuf_buffer *buffer)
5087{
5088 struct weston_renderer *renderer;
5089
5090 renderer = compositor->renderer;
5091
5092 if (renderer->import_dmabuf == NULL)
5093 return false;
5094
5095 return renderer->import_dmabuf(compositor, buffer);
5096}
5097
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005098WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005099weston_version(int *major, int *minor, int *micro)
5100{
5101 *major = WESTON_VERSION_MAJOR;
5102 *minor = WESTON_VERSION_MINOR;
5103 *micro = WESTON_VERSION_MICRO;
5104}
5105
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005106WL_EXPORT void *
5107weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005108{
Derek Foreman3f86e502015-06-08 11:46:54 -05005109 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005110 char path[PATH_MAX];
5111 void *module, *init;
5112
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005113 if (name == NULL)
5114 return NULL;
5115
Derek Foreman3f86e502015-06-08 11:46:54 -05005116 if (name[0] != '/') {
5117 if (builddir)
5118 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
5119 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03005120 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005121 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02005122 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005123 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005124
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005125 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
5126 if (module) {
5127 weston_log("Module '%s' already loaded\n", path);
5128 dlclose(module);
5129 return NULL;
5130 }
5131
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005132 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04005133 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005134 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005135 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005136 return NULL;
5137 }
5138
5139 init = dlsym(module, entrypoint);
5140 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005141 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00005142 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005143 return NULL;
5144 }
5145
5146 return init;
5147}
5148
Giulio Camuffo459137b2014-10-11 23:56:24 +03005149
5150/** Destroys the compositor.
5151 *
5152 * This function cleans up the compositor state and destroys it.
5153 *
5154 * \param compositor The compositor to be destroyed.
5155 */
5156WL_EXPORT void
5157weston_compositor_destroy(struct weston_compositor *compositor)
5158{
5159 /* prevent further rendering while shutting down */
5160 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5161
5162 wl_signal_emit(&compositor->destroy_signal, compositor);
5163
5164 weston_compositor_xkb_destroy(compositor);
5165
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005166 if (compositor->backend)
5167 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005168
5169 weston_plugin_api_destroy_list(compositor);
5170
Giulio Camuffo459137b2014-10-11 23:56:24 +03005171 free(compositor);
5172}
5173
5174/** Instruct the compositor to exit.
5175 *
5176 * This functions does not directly destroy the compositor object, it merely
5177 * command it to start the tear down process. It is not guaranteed that the
5178 * tear down will happen immediately.
5179 *
5180 * \param compositor The compositor to tear down.
5181 */
5182WL_EXPORT void
5183weston_compositor_exit(struct weston_compositor *compositor)
5184{
5185 compositor->exit(compositor);
5186}
5187
5188/** Return the user data stored in the compositor.
5189 *
5190 * This function returns the user data pointer set with user_data parameter
5191 * to the \ref weston_compositor_create function.
5192 */
5193WL_EXPORT void *
5194weston_compositor_get_user_data(struct weston_compositor *compositor)
5195{
5196 return compositor->user_data;
5197}
Pekka Paalanendd186732016-06-03 14:49:54 +03005198
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005199static const char * const backend_map[] = {
5200 [WESTON_BACKEND_DRM] = "drm-backend.so",
5201 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5202 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5203 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5204 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5205 [WESTON_BACKEND_X11] = "x11-backend.so",
5206};
5207
Pekka Paalanendd186732016-06-03 14:49:54 +03005208/** Load a backend into a weston_compositor
5209 *
5210 * A backend must be loaded to make a weston_compositor work. A backend
5211 * provides input and output capabilities, and determines the renderer to use.
5212 *
5213 * \param compositor A compositor that has not had a backend loaded yet.
5214 * \param backend Name of the backend file.
5215 * \param config_base A pointer to a backend-specific configuration
5216 * structure's 'base' member.
5217 *
5218 * \return 0 on success, or -1 on error.
5219 */
5220WL_EXPORT int
5221weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005222 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005223 struct weston_backend_config *config_base)
5224{
5225 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005226 struct weston_backend_config *config_base);
5227
Quentin Glidic887c0182016-07-10 11:00:53 +02005228 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005229 return -1;
5230
5231 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005232 if (!backend_init)
5233 return -1;
5234
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005235 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005236}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005237
5238WL_EXPORT int
5239weston_compositor_load_xwayland(struct weston_compositor *compositor)
5240{
5241 int (*module_init)(struct weston_compositor *ec,
5242 int *argc, char *argv[]);
5243 int argc = 0;
5244
5245 module_init = weston_load_module("xwayland.so", "module_init");
5246 if (!module_init)
5247 return -1;
5248 if (module_init(compositor, &argc, NULL) < 0)
5249 return -1;
5250 return 0;
5251}