blob: 7f922884ca638edd17a0258ab0fb89e58e926f66 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050026 */
27
Kristian Høgsberga9410222011-01-14 17:22:35 -050028#include "config.h"
29
Daniel Stoneb7452fe2012-06-01 12:14:06 +010030#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010035#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020037#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010039#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040040#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020041#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020042#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020043#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050045#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040046#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040047#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040048#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050049#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040050#include <sys/time.h>
51#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030052#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050053
Pekka Paalanenb5026542014-11-12 15:09:24 +020054#include "timeline.h"
55
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030057#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020058#include "presentation-time-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070059#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070060#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070061#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030062#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040063#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050064#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020065#include "plugin-registry.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066
Pekka Paalanen0513a952014-05-21 16:17:27 +030067#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
68
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020069static void
Alexander Larsson0b135062013-05-28 16:23:36 +020070weston_output_transform_scale_init(struct weston_output *output,
71 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020072
Rob Bradford27b17932013-06-26 18:08:46 +010073static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050074weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010075
Derek Foreman6ae7bc92014-11-04 10:47:33 -060076static void weston_mode_switch_finish(struct weston_output *output,
77 int mode_changed,
78 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080079{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020080 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020081 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020082 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060083 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020084
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020085 pixman_region32_init(&old_output_region);
86 pixman_region32_copy(&old_output_region, &output->region);
87
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020089 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020090
91 pixman_region32_init(&output->previous_damage);
92 pixman_region32_init_rect(&output->region, output->x, output->y,
93 output->width, output->height);
94
95 weston_output_update_matrix(output);
96
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020097 /* If a pointer falls outside the outputs new geometry, move it to its
98 * lower-right corner */
99 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500100 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200101 int32_t x, y;
102
103 if (!pointer)
104 continue;
105
106 x = wl_fixed_to_int(pointer->x);
107 y = wl_fixed_to_int(pointer->y);
108
109 if (!pixman_region32_contains_point(&old_output_region,
110 x, y, NULL) ||
111 pixman_region32_contains_point(&output->region,
112 x, y, NULL))
113 continue;
114
115 if (x >= output->x + output->width)
116 x = output->x + output->width - 1;
117 if (y >= output->y + output->height)
118 y = output->y + output->height - 1;
119
120 pointer->x = wl_fixed_from_int(x);
121 pointer->y = wl_fixed_from_int(y);
122 }
123
124 pixman_region32_fini(&old_output_region);
125
Derek Foremandd4cd332014-11-10 10:29:59 -0600126 if (!mode_changed && !scale_changed)
127 return;
128
Hardening57388e42013-09-18 23:56:36 +0200129 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600130 wl_resource_for_each(resource, &output->resource_list) {
131 if (mode_changed) {
132 wl_output_send_mode(resource,
133 output->current_mode->flags,
134 output->current_mode->width,
135 output->current_mode->height,
136 output->current_mode->refresh);
137 }
Hardening57388e42013-09-18 23:56:36 +0200138
Derek Foreman41bdc272014-11-05 13:26:57 -0600139 version = wl_resource_get_version(resource);
140 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600141 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200142
Derek Foreman41bdc272014-11-05 13:26:57 -0600143 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
144 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600145 }
146}
147
David Fort0de859e2016-05-27 23:22:57 +0200148
149static void
150weston_compositor_reflow_outputs(struct weston_compositor *compositor,
151 struct weston_output *resized_output, int delta_width);
152
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600153WL_EXPORT int
154weston_output_mode_set_native(struct weston_output *output,
155 struct weston_mode *mode,
156 int32_t scale)
157{
158 int ret;
159 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200160 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600161
162 if (!output->switch_mode)
163 return -1;
164
165 if (!output->original_mode) {
166 mode_changed = 1;
167 ret = output->switch_mode(output, mode);
168 if (ret < 0)
169 return ret;
170 if (output->current_scale != scale) {
171 scale_changed = 1;
172 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200173 }
174 }
175
David Fort0de859e2016-05-27 23:22:57 +0200176 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600177 output->native_mode = mode;
178 output->native_scale = scale;
179
180 weston_mode_switch_finish(output, mode_changed, scale_changed);
181
David Fort0de859e2016-05-27 23:22:57 +0200182 if (mode_changed || scale_changed) {
183 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
184
185 wl_signal_emit(&output->compositor->output_resized_signal, output);
186 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600187 return 0;
188}
189
190WL_EXPORT int
191weston_output_mode_switch_to_native(struct weston_output *output)
192{
193 int ret;
194 int mode_changed = 0, scale_changed = 0;
195
196 if (!output->switch_mode)
197 return -1;
198
199 if (!output->original_mode) {
200 weston_log("already in the native mode\n");
201 return -1;
202 }
203 /* the non fullscreen clients haven't seen a mode set since we
204 * switched into a temporary, so we need to notify them if the
205 * mode at that time is different from the native mode now.
206 */
207 mode_changed = (output->original_mode != output->native_mode);
208 scale_changed = (output->original_scale != output->native_scale);
209
210 ret = output->switch_mode(output, output->native_mode);
211 if (ret < 0)
212 return ret;
213
214 output->current_scale = output->native_scale;
215
216 output->original_mode = NULL;
217 output->original_scale = 0;
218
219 weston_mode_switch_finish(output, mode_changed, scale_changed);
220
221 return 0;
222}
223
224WL_EXPORT int
225weston_output_mode_switch_to_temporary(struct weston_output *output,
226 struct weston_mode *mode,
227 int32_t scale)
228{
229 int ret;
230
231 if (!output->switch_mode)
232 return -1;
233
234 /* original_mode is the last mode non full screen clients have seen,
235 * so we shouldn't change it if we already have one set.
236 */
237 if (!output->original_mode) {
238 output->original_mode = output->native_mode;
239 output->original_scale = output->native_scale;
240 }
241 ret = output->switch_mode(output, mode);
242 if (ret < 0)
243 return ret;
244
245 output->current_scale = scale;
246
247 weston_mode_switch_finish(output, 0, 0);
248
249 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800250}
251
Benjamin Franzke06286262011-05-06 19:12:33 +0200252static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300253region_init_infinite(pixman_region32_t *region)
254{
255 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
256 UINT32_MAX, UINT32_MAX);
257}
258
Pekka Paalanene67858b2013-04-25 13:57:42 +0300259static struct weston_subsurface *
260weston_surface_to_subsurface(struct weston_surface *surface);
261
Jason Ekstranda7af7042013-10-12 22:38:11 -0500262WL_EXPORT struct weston_view *
263weston_view_create(struct weston_surface *surface)
264{
265 struct weston_view *view;
266
Bryce Harringtonde16d892014-11-20 22:21:57 -0800267 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500268 if (view == NULL)
269 return NULL;
270
271 view->surface = surface;
272
Jason Ekstranda7af7042013-10-12 22:38:11 -0500273 /* Assign to surface */
274 wl_list_insert(&surface->views, &view->surface_link);
275
276 wl_signal_init(&view->destroy_signal);
277 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300278 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279
Jason Ekstranda7af7042013-10-12 22:38:11 -0500280 pixman_region32_init(&view->clip);
281
282 view->alpha = 1.0;
283 pixman_region32_init(&view->transform.opaque);
284
285 wl_list_init(&view->geometry.transformation_list);
286 wl_list_insert(&view->geometry.transformation_list,
287 &view->transform.position.link);
288 weston_matrix_init(&view->transform.position.matrix);
289 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200290 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500291 pixman_region32_init(&view->transform.boundingbox);
292 view->transform.dirty = 1;
293
Jason Ekstranda7af7042013-10-12 22:38:11 -0500294 return view;
295}
296
Jason Ekstrand108865d2014-06-26 10:04:49 -0700297struct weston_frame_callback {
298 struct wl_resource *resource;
299 struct wl_list link;
300};
301
Pekka Paalanen133e4392014-09-23 22:08:46 -0400302struct weston_presentation_feedback {
303 struct wl_resource *resource;
304
305 /* XXX: could use just wl_resource_get_link() instead */
306 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200307
308 /* The per-surface feedback flags */
309 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400310};
311
312static void
313weston_presentation_feedback_discard(
314 struct weston_presentation_feedback *feedback)
315{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200316 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400317 wl_resource_destroy(feedback->resource);
318}
319
320static void
321weston_presentation_feedback_discard_list(struct wl_list *list)
322{
323 struct weston_presentation_feedback *feedback, *tmp;
324
325 wl_list_for_each_safe(feedback, tmp, list, link)
326 weston_presentation_feedback_discard(feedback);
327}
328
329static void
330weston_presentation_feedback_present(
331 struct weston_presentation_feedback *feedback,
332 struct weston_output *output,
333 uint32_t refresh_nsec,
334 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200335 uint64_t seq,
336 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400337{
338 struct wl_client *client = wl_resource_get_client(feedback->resource);
339 struct wl_resource *o;
340 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400341
342 wl_resource_for_each(o, &output->resource_list) {
343 if (wl_resource_get_client(o) != client)
344 continue;
345
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200346 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400347 }
348
349 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200350 wp_presentation_feedback_send_presented(feedback->resource,
351 secs >> 32, secs & 0xffffffff,
352 ts->tv_nsec,
353 refresh_nsec,
354 seq >> 32, seq & 0xffffffff,
355 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400356 wl_resource_destroy(feedback->resource);
357}
358
359static void
360weston_presentation_feedback_present_list(struct wl_list *list,
361 struct weston_output *output,
362 uint32_t refresh_nsec,
363 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200364 uint64_t seq,
365 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400366{
367 struct weston_presentation_feedback *feedback, *tmp;
368
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200369 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200370 wl_list_empty(list));
371
Pekka Paalanen133e4392014-09-23 22:08:46 -0400372 wl_list_for_each_safe(feedback, tmp, list, link)
373 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200374 refresh_nsec, ts, seq,
375 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400376}
377
Jason Ekstrand7b982072014-05-20 14:33:03 -0500378static void
379surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
380{
381 struct weston_surface_state *state =
382 container_of(listener, struct weston_surface_state,
383 buffer_destroy_listener);
384
385 state->buffer = NULL;
386}
387
388static void
389weston_surface_state_init(struct weston_surface_state *state)
390{
391 state->newly_attached = 0;
392 state->buffer = NULL;
393 state->buffer_destroy_listener.notify =
394 surface_state_handle_buffer_destroy;
395 state->sx = 0;
396 state->sy = 0;
397
Derek Foreman152254b2015-11-26 14:17:48 -0600398 pixman_region32_init(&state->damage_surface);
399 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500400 pixman_region32_init(&state->opaque);
401 region_init_infinite(&state->input);
402
403 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400404 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500405
406 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
407 state->buffer_viewport.buffer.scale = 1;
408 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
409 state->buffer_viewport.surface.width = -1;
410 state->buffer_viewport.changed = 0;
411}
412
413static void
414weston_surface_state_fini(struct weston_surface_state *state)
415{
416 struct weston_frame_callback *cb, *next;
417
418 wl_list_for_each_safe(cb, next,
419 &state->frame_callback_list, link)
420 wl_resource_destroy(cb->resource);
421
Pekka Paalanen133e4392014-09-23 22:08:46 -0400422 weston_presentation_feedback_discard_list(&state->feedback_list);
423
Jason Ekstrand7b982072014-05-20 14:33:03 -0500424 pixman_region32_fini(&state->input);
425 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600426 pixman_region32_fini(&state->damage_surface);
427 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500428
429 if (state->buffer)
430 wl_list_remove(&state->buffer_destroy_listener.link);
431 state->buffer = NULL;
432}
433
434static void
435weston_surface_state_set_buffer(struct weston_surface_state *state,
436 struct weston_buffer *buffer)
437{
438 if (state->buffer == buffer)
439 return;
440
441 if (state->buffer)
442 wl_list_remove(&state->buffer_destroy_listener.link);
443 state->buffer = buffer;
444 if (state->buffer)
445 wl_signal_add(&state->buffer->destroy_signal,
446 &state->buffer_destroy_listener);
447}
448
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500449WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500450weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500451{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500452 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400453
Bryce Harringtonde16d892014-11-20 22:21:57 -0800454 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400455 if (surface == NULL)
456 return NULL;
457
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500458 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800459 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500460
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500461 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200462 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400463
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200464 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
465 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200466 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
467 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500468
469 weston_surface_state_init(&surface->pending);
470
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400471 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500472 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300473 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400474
Jason Ekstranda7af7042013-10-12 22:38:11 -0500475 wl_list_init(&surface->views);
476
477 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400478 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500479
Pekka Paalanene67858b2013-04-25 13:57:42 +0300480 wl_list_init(&surface->subsurface_list);
481 wl_list_init(&surface->subsurface_list_pending);
482
Jason Ekstrand1e059042014-10-16 10:55:19 -0500483 weston_matrix_init(&surface->buffer_to_surface_matrix);
484 weston_matrix_init(&surface->surface_to_buffer_matrix);
485
Jonas Ådahld3414f22016-07-22 17:56:31 +0800486 wl_list_init(&surface->pointer_constraints);
487
Bryce Harringtonf8300c82016-08-10 17:25:22 -0700488 surface->inhibit_idling = false;
489
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400490 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500491}
492
Alex Wu8811bf92012-02-28 18:07:54 +0800493WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500494weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200495 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500496{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100497 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500498}
499
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400500WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500501weston_view_to_global_float(struct weston_view *view,
502 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200503{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500504 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200505 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
506
Jason Ekstranda7af7042013-10-12 22:38:11 -0500507 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200508
509 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200510 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700511 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200512 v.f[3]);
513 *x = 0;
514 *y = 0;
515 return;
516 }
517
518 *x = v.f[0] / v.f[3];
519 *y = v.f[1] / v.f[3];
520 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500521 *x = sx + view->geometry.x;
522 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200523 }
524}
525
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500526WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200527weston_transformed_coord(int width, int height,
528 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200529 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200530 float sx, float sy, float *bx, float *by)
531{
532 switch (transform) {
533 case WL_OUTPUT_TRANSFORM_NORMAL:
534 default:
535 *bx = sx;
536 *by = sy;
537 break;
538 case WL_OUTPUT_TRANSFORM_FLIPPED:
539 *bx = width - sx;
540 *by = sy;
541 break;
542 case WL_OUTPUT_TRANSFORM_90:
543 *bx = height - sy;
544 *by = sx;
545 break;
546 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
547 *bx = height - sy;
548 *by = width - sx;
549 break;
550 case WL_OUTPUT_TRANSFORM_180:
551 *bx = width - sx;
552 *by = height - sy;
553 break;
554 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
555 *bx = sx;
556 *by = height - sy;
557 break;
558 case WL_OUTPUT_TRANSFORM_270:
559 *bx = sy;
560 *by = width - sx;
561 break;
562 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
563 *bx = sy;
564 *by = sx;
565 break;
566 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200567
568 *bx *= scale;
569 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200570}
571
572WL_EXPORT pixman_box32_t
573weston_transformed_rect(int width, int height,
574 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200575 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200576 pixman_box32_t rect)
577{
578 float x1, x2, y1, y2;
579
580 pixman_box32_t ret;
581
Alexander Larsson4ea95522013-05-22 14:41:37 +0200582 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200583 rect.x1, rect.y1, &x1, &y1);
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.x2, rect.y2, &x2, &y2);
586
587 if (x1 <= x2) {
588 ret.x1 = x1;
589 ret.x2 = x2;
590 } else {
591 ret.x1 = x2;
592 ret.x2 = x1;
593 }
594
595 if (y1 <= y2) {
596 ret.y1 = y1;
597 ret.y2 = y2;
598 } else {
599 ret.y1 = y2;
600 ret.y2 = y1;
601 }
602
603 return ret;
604}
605
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600606/** Transform a region by a matrix, restricted to axis-aligned transformations
607 *
608 * Warning: This function does not work for projective, affine, or matrices
609 * that encode arbitrary rotations. Only 90-degree step rotations are
610 * supported.
611 */
612WL_EXPORT void
613weston_matrix_transform_region(pixman_region32_t *dest,
614 struct weston_matrix *matrix,
615 pixman_region32_t *src)
616{
617 pixman_box32_t *src_rects, *dest_rects;
618 int nrects, i;
619
620 src_rects = pixman_region32_rectangles(src, &nrects);
621 dest_rects = malloc(nrects * sizeof(*dest_rects));
622 if (!dest_rects)
623 return;
624
625 for (i = 0; i < nrects; i++) {
626 struct weston_vector vec1 = {{
627 src_rects[i].x1, src_rects[i].y1, 0, 1
628 }};
629 weston_matrix_transform(matrix, &vec1);
630 vec1.f[0] /= vec1.f[3];
631 vec1.f[1] /= vec1.f[3];
632
633 struct weston_vector vec2 = {{
634 src_rects[i].x2, src_rects[i].y2, 0, 1
635 }};
636 weston_matrix_transform(matrix, &vec2);
637 vec2.f[0] /= vec2.f[3];
638 vec2.f[1] /= vec2.f[3];
639
640 if (vec1.f[0] < vec2.f[0]) {
641 dest_rects[i].x1 = floor(vec1.f[0]);
642 dest_rects[i].x2 = ceil(vec2.f[0]);
643 } else {
644 dest_rects[i].x1 = floor(vec2.f[0]);
645 dest_rects[i].x2 = ceil(vec1.f[0]);
646 }
647
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600648 if (vec1.f[1] < vec2.f[1]) {
649 dest_rects[i].y1 = floor(vec1.f[1]);
650 dest_rects[i].y2 = ceil(vec2.f[1]);
651 } else {
652 dest_rects[i].y1 = floor(vec2.f[1]);
653 dest_rects[i].y2 = ceil(vec1.f[1]);
654 }
655 }
656
657 pixman_region32_clear(dest);
658 pixman_region32_init_rects(dest, dest_rects, nrects);
659 free(dest_rects);
660}
661
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200662WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500663weston_transformed_region(int width, int height,
664 enum wl_output_transform transform,
665 int32_t scale,
666 pixman_region32_t *src, pixman_region32_t *dest)
667{
668 pixman_box32_t *src_rects, *dest_rects;
669 int nrects, i;
670
671 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
672 if (src != dest)
673 pixman_region32_copy(dest, src);
674 return;
675 }
676
677 src_rects = pixman_region32_rectangles(src, &nrects);
678 dest_rects = malloc(nrects * sizeof(*dest_rects));
679 if (!dest_rects)
680 return;
681
682 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
683 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
684 } else {
685 for (i = 0; i < nrects; i++) {
686 switch (transform) {
687 default:
688 case WL_OUTPUT_TRANSFORM_NORMAL:
689 dest_rects[i].x1 = src_rects[i].x1;
690 dest_rects[i].y1 = src_rects[i].y1;
691 dest_rects[i].x2 = src_rects[i].x2;
692 dest_rects[i].y2 = src_rects[i].y2;
693 break;
694 case WL_OUTPUT_TRANSFORM_90:
695 dest_rects[i].x1 = height - src_rects[i].y2;
696 dest_rects[i].y1 = src_rects[i].x1;
697 dest_rects[i].x2 = height - src_rects[i].y1;
698 dest_rects[i].y2 = src_rects[i].x2;
699 break;
700 case WL_OUTPUT_TRANSFORM_180:
701 dest_rects[i].x1 = width - src_rects[i].x2;
702 dest_rects[i].y1 = height - src_rects[i].y2;
703 dest_rects[i].x2 = width - src_rects[i].x1;
704 dest_rects[i].y2 = height - src_rects[i].y1;
705 break;
706 case WL_OUTPUT_TRANSFORM_270:
707 dest_rects[i].x1 = src_rects[i].y1;
708 dest_rects[i].y1 = width - src_rects[i].x2;
709 dest_rects[i].x2 = src_rects[i].y2;
710 dest_rects[i].y2 = width - src_rects[i].x1;
711 break;
712 case WL_OUTPUT_TRANSFORM_FLIPPED:
713 dest_rects[i].x1 = width - src_rects[i].x2;
714 dest_rects[i].y1 = src_rects[i].y1;
715 dest_rects[i].x2 = width - src_rects[i].x1;
716 dest_rects[i].y2 = src_rects[i].y2;
717 break;
718 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
719 dest_rects[i].x1 = height - src_rects[i].y2;
720 dest_rects[i].y1 = width - src_rects[i].x2;
721 dest_rects[i].x2 = height - src_rects[i].y1;
722 dest_rects[i].y2 = width - src_rects[i].x1;
723 break;
724 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
725 dest_rects[i].x1 = src_rects[i].x1;
726 dest_rects[i].y1 = height - src_rects[i].y2;
727 dest_rects[i].x2 = src_rects[i].x2;
728 dest_rects[i].y2 = height - src_rects[i].y1;
729 break;
730 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
731 dest_rects[i].x1 = src_rects[i].y1;
732 dest_rects[i].y1 = src_rects[i].x1;
733 dest_rects[i].x2 = src_rects[i].y2;
734 dest_rects[i].y2 = src_rects[i].x2;
735 break;
736 }
737 }
738 }
739
740 if (scale != 1) {
741 for (i = 0; i < nrects; i++) {
742 dest_rects[i].x1 *= scale;
743 dest_rects[i].x2 *= scale;
744 dest_rects[i].y1 *= scale;
745 dest_rects[i].y2 *= scale;
746 }
747 }
748
749 pixman_region32_clear(dest);
750 pixman_region32_init_rects(dest, dest_rects, nrects);
751 free(dest_rects);
752}
753
Jonny Lamb74130762013-11-26 18:19:46 +0100754static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300755viewport_surface_to_buffer(struct weston_surface *surface,
756 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100757{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200758 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200759 double src_width, src_height;
760 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200761
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200762 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
763 if (vp->surface.width == -1) {
764 *bx = sx;
765 *by = sy;
766 return;
767 }
Jonny Lamb74130762013-11-26 18:19:46 +0100768
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200769 src_x = 0.0;
770 src_y = 0.0;
771 src_width = surface->width_from_buffer;
772 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100773 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200774 src_x = wl_fixed_to_double(vp->buffer.src_x);
775 src_y = wl_fixed_to_double(vp->buffer.src_y);
776 src_width = wl_fixed_to_double(vp->buffer.src_width);
777 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100778 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200779
780 *bx = sx * src_width / surface->width + src_x;
781 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100782}
783
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500784WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200785weston_surface_to_buffer_float(struct weston_surface *surface,
786 float sx, float sy, float *bx, float *by)
787{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200788 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
789
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300790 /* first transform coordinates if the viewport is set */
791 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100792
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500793 weston_transformed_coord(surface->width_from_buffer,
794 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200795 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100796 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200797}
798
Derek Foreman99739672015-12-03 16:38:11 -0600799/** Transform a rectangle from surface coordinates to buffer coordinates
800 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300801 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600802 * from.
803 * \param rect The rectangle to transform.
804 * \return The transformed rectangle.
805 *
806 * Viewport and buffer transformations can only do translation, scaling,
807 * and rotations in 90-degree steps. Therefore the only loss in the
808 * conversion is coordinate rounding.
809 *
810 * However, some coordinate rounding takes place as an intermediate
811 * step before the buffer scale factor is applied, so the rectangle
812 * boundary may not be exactly as expected.
813 *
814 * This is OK for damage tracking since a little extra coverage is
815 * not a problem.
816 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200817WL_EXPORT pixman_box32_t
818weston_surface_to_buffer_rect(struct weston_surface *surface,
819 pixman_box32_t rect)
820{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200821 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100822 float xf, yf;
823
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300824 /* first transform box coordinates if the viewport is set */
825 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100826 rect.x1 = floorf(xf);
827 rect.y1 = floorf(yf);
828
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300829 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600830 rect.x2 = ceilf(xf);
831 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100832
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500833 return weston_transformed_rect(surface->width_from_buffer,
834 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200835 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200836 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200837}
838
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200839/** Transform a region from surface coordinates to buffer coordinates
840 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300841 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200842 * from.
843 * \param surface_region[in] The region in surface coordinates.
844 * \param buffer_region[out] The region converted to buffer coordinates.
845 *
846 * Buffer_region must be init'd, but will be completely overwritten.
847 *
848 * Viewport and buffer transformations can only do translation, scaling,
849 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600850 * conversion is from the coordinate rounding that takes place in
851 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200852 */
853WL_EXPORT void
854weston_surface_to_buffer_region(struct weston_surface *surface,
855 pixman_region32_t *surface_region,
856 pixman_region32_t *buffer_region)
857{
858 pixman_box32_t *src_rects, *dest_rects;
859 int nrects, i;
860
861 src_rects = pixman_region32_rectangles(surface_region, &nrects);
862 dest_rects = malloc(nrects * sizeof(*dest_rects));
863 if (!dest_rects)
864 return;
865
866 for (i = 0; i < nrects; i++) {
867 dest_rects[i] = weston_surface_to_buffer_rect(surface,
868 src_rects[i]);
869 }
870
871 pixman_region32_fini(buffer_region);
872 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
873 free(dest_rects);
874}
875
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200876WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500877weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400878 struct weston_plane *plane)
879{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500880 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400881 return;
882
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883 weston_view_damage_below(view);
884 view->plane = plane;
885 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400886}
887
Pekka Paalanen51723d52015-02-17 13:10:01 +0200888/** Inflict damage on the plane where the view is visible.
889 *
890 * \param view The view that causes the damage.
891 *
892 * If the view is currently on a plane (including the primary plane),
893 * take the view's boundingbox, subtract all the opaque views that cover it,
894 * and add the remaining region as damage to the plane. This corresponds
895 * to the damage inflicted to the plane if this view disappeared.
896 *
897 * A repaint is scheduled for this view.
898 *
899 * The region of all opaque views covering this view is stored in
900 * weston_view::clip and updated by view_accumulate_damage() during
901 * weston_output_repaint(). Specifically, that region matches the
902 * scenegraph as it was last painted.
903 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400904WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500905weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200906{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500907 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200908
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500909 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200910 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800912 if (view->plane)
913 pixman_region32_union(&view->plane->damage,
914 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500915 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800916 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200917}
918
Bryce Harrington3f650b82015-12-23 11:01:58 -0800919/**
920 * \param es The surface
921 * \param mask The new set of outputs for the surface
922 *
923 * Sets the surface's set of outputs to the ones specified by
924 * the new output mask provided. Identifies the outputs that
925 * have changed, the posts enter and leave events for these
926 * outputs as appropriate.
927 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400928static void
929weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
930{
931 uint32_t different = es->output_mask ^ mask;
932 uint32_t entered = mask & different;
933 uint32_t left = es->output_mask & different;
934 struct weston_output *output;
935 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500936 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400937
938 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500939 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400940 return;
941 if (different == 0)
942 return;
943
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500944 client = wl_resource_get_client(es->resource);
945
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400946 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800947 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400948 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500949 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400950 client);
951 if (resource == NULL)
952 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800953 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500954 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800955 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500956 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400957 }
958}
959
Bryce Harrington3f650b82015-12-23 11:01:58 -0800960/** Recalculate which output(s) the surface has views displayed on
961 *
962 * \param es The surface to remap to outputs
963 *
964 * Finds the output that is showing the largest amount of one
965 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300966 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800967 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300968 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800969 * in the output_mask for the surface.
970 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400971static void
972weston_surface_assign_output(struct weston_surface *es)
973{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974 struct weston_output *new_output;
975 struct weston_view *view;
976 pixman_region32_t region;
977 uint32_t max, area, mask;
978 pixman_box32_t *e;
979
980 new_output = NULL;
981 max = 0;
982 mask = 0;
983 pixman_region32_init(&region);
984 wl_list_for_each(view, &es->views, surface_link) {
985 if (!view->output)
986 continue;
987
988 pixman_region32_intersect(&region, &view->transform.boundingbox,
989 &view->output->region);
990
991 e = pixman_region32_extents(&region);
992 area = (e->x2 - e->x1) * (e->y2 - e->y1);
993
994 mask |= view->output_mask;
995
996 if (area >= max) {
997 new_output = view->output;
998 max = area;
999 }
1000 }
1001 pixman_region32_fini(&region);
1002
1003 es->output = new_output;
1004 weston_surface_update_output_mask(es, mask);
1005}
1006
Bryce Harrington3f650b82015-12-23 11:01:58 -08001007/** Recalculate which output(s) the view is displayed on
1008 *
1009 * \param ev The view to remap to outputs
1010 *
1011 * Identifies the set of outputs that the view is visible on,
1012 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001013 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001014 *
1015 * Also does the same for the view's surface. See
1016 * weston_surface_assign_output().
1017 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001018static void
1019weston_view_assign_output(struct weston_view *ev)
1020{
1021 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001022 struct weston_output *output, *new_output;
1023 pixman_region32_t region;
1024 uint32_t max, area, mask;
1025 pixman_box32_t *e;
1026
1027 new_output = NULL;
1028 max = 0;
1029 mask = 0;
1030 pixman_region32_init(&region);
1031 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001032 if (output->destroying)
1033 continue;
1034
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001036 &output->region);
1037
1038 e = pixman_region32_extents(&region);
1039 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1040
1041 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001042 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001043
1044 if (area >= max) {
1045 new_output = output;
1046 max = area;
1047 }
1048 }
1049 pixman_region32_fini(&region);
1050
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 ev->output = new_output;
1052 ev->output_mask = mask;
1053
1054 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001055}
1056
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001057static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001058weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1059 int from_x, int from_y, int *to_x, int *to_y)
1060{
1061 float x, y;
1062
1063 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1064 weston_view_from_global_float(to, x, y, &x, &y);
1065
1066 *to_x = round(x);
1067 *to_y = round(y);
1068}
1069
1070static void
1071weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1072{
1073 pixman_box32_t *a;
1074 pixman_box32_t b;
1075
1076 a = pixman_region32_extents(&from->geometry.scissor);
1077
1078 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1079 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1080
1081 pixman_region32_fini(&to->geometry.scissor);
1082 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1083}
1084
1085static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001086view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001087 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001088{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001089 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1090 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001091 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001092 { inbox->x1, inbox->y1 },
1093 { inbox->x1, inbox->y2 },
1094 { inbox->x2, inbox->y1 },
1095 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001096 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001097 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001098 int i;
1099
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001100 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001101 /* avoid rounding empty bbox to 1x1 */
1102 pixman_region32_init(bbox);
1103 return;
1104 }
1105
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001106 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001107 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001108 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001109 if (x < min_x)
1110 min_x = x;
1111 if (x > max_x)
1112 max_x = x;
1113 if (y < min_y)
1114 min_y = y;
1115 if (y > max_y)
1116 max_y = y;
1117 }
1118
Pekka Paalanen219b9822012-02-08 15:38:37 +02001119 int_x = floorf(min_x);
1120 int_y = floorf(min_y);
1121 pixman_region32_init_rect(bbox, int_x, int_y,
1122 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001123}
1124
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001125static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001127{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001129
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001130 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001131 view->geometry.x = roundf(view->geometry.x);
1132 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001133
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001134 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1136 view->transform.position.matrix.d[12] = view->geometry.x;
1137 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001138
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001140
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 view->transform.inverse = view->transform.position.matrix;
1142 view->transform.inverse.d[12] = -view->geometry.x;
1143 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001144
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001146 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001147 view->surface->width,
1148 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001149 if (view->geometry.scissor_enabled)
1150 pixman_region32_intersect(&view->transform.boundingbox,
1151 &view->transform.boundingbox,
1152 &view->geometry.scissor);
1153
1154 pixman_region32_translate(&view->transform.boundingbox,
1155 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001156
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157 if (view->alpha == 1.0) {
1158 pixman_region32_copy(&view->transform.opaque,
1159 &view->surface->opaque);
1160 pixman_region32_translate(&view->transform.opaque,
1161 view->geometry.x,
1162 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001163 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001164}
1165
1166static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001168{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 struct weston_view *parent = view->geometry.parent;
1170 struct weston_matrix *matrix = &view->transform.matrix;
1171 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001172 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001173 pixman_region32_t surfregion;
1174 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001175
Jason Ekstranda7af7042013-10-12 22:38:11 -05001176 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001177
1178 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001179 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1180 view->transform.position.matrix.d[12] = view->geometry.x;
1181 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001182
1183 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001185 weston_matrix_multiply(matrix, &tform->matrix);
1186
Pekka Paalanen483243f2013-03-08 14:56:50 +02001187 if (parent)
1188 weston_matrix_multiply(matrix, &parent->transform.matrix);
1189
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001190 if (weston_matrix_invert(inverse, matrix) < 0) {
1191 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 weston_log("error: weston_view %p"
1193 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001194 return -1;
1195 }
1196
Pekka Paalanen380adf52015-02-16 14:39:11 +02001197 pixman_region32_init_rect(&surfregion, 0, 0,
1198 view->surface->width, view->surface->height);
1199 if (view->geometry.scissor_enabled)
1200 pixman_region32_intersect(&surfregion, &surfregion,
1201 &view->geometry.scissor);
1202 surfbox = pixman_region32_extents(&surfregion);
1203
1204 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1205 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001206
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001207 return 0;
1208}
1209
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001210static struct weston_layer *
1211get_view_layer(struct weston_view *view)
1212{
1213 if (view->parent_view)
1214 return get_view_layer(view->parent_view);
1215 return view->layer_link.layer;
1216}
1217
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001218WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001220{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001222 struct weston_layer *layer;
1223 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001224
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001226 return;
1227
Pekka Paalanen483243f2013-03-08 14:56:50 +02001228 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001230
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001232
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001234
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 pixman_region32_fini(&view->transform.boundingbox);
1236 pixman_region32_fini(&view->transform.opaque);
1237 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001238
Pekka Paalanencd403622012-01-25 13:37:39 +02001239 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 if (view->geometry.transformation_list.next ==
1241 &view->transform.position.link &&
1242 view->geometry.transformation_list.prev ==
1243 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001244 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001246 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 if (weston_view_update_transform_enable(view) < 0)
1248 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001249 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001250
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001251 layer = get_view_layer(view);
1252 if (layer) {
1253 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001254 pixman_region32_intersect(&view->transform.boundingbox,
1255 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001256 pixman_region32_intersect(&view->transform.opaque,
1257 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001258 pixman_region32_fini(&mask);
1259 }
1260
Pekka Paalanen380adf52015-02-16 14:39:11 +02001261 if (parent) {
1262 if (parent->geometry.scissor_enabled) {
1263 view->geometry.scissor_enabled = true;
1264 weston_view_transfer_scissor(parent, view);
1265 } else {
1266 view->geometry.scissor_enabled = false;
1267 }
1268 }
1269
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001271
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001273
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 wl_signal_emit(&view->surface->compositor->transform_signal,
1275 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001276}
1277
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001278WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001280{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001282
1283 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 * The invariant: if view->geometry.dirty, then all views
1285 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001286 * Corollary: if not parent->geometry.dirty, then all ancestors
1287 * are not dirty.
1288 */
1289
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001291 return;
1292
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001294
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001296 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001298}
1299
1300WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301weston_view_to_global_fixed(struct weston_view *view,
1302 wl_fixed_t vx, wl_fixed_t vy,
1303 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001304{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001305 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001306
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 weston_view_to_global_float(view,
1308 wl_fixed_to_double(vx),
1309 wl_fixed_to_double(vy),
1310 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001311 *x = wl_fixed_from_double(xf);
1312 *y = wl_fixed_from_double(yf);
1313}
1314
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001315WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316weston_view_from_global_float(struct weston_view *view,
1317 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001318{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001320 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1321
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001323
1324 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001325 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001327 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 *vx = 0;
1329 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001330 return;
1331 }
1332
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 *vx = v.f[0] / v.f[3];
1334 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001335 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 *vx = x - view->geometry.x;
1337 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001338 }
1339}
1340
1341WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342weston_view_from_global_fixed(struct weston_view *view,
1343 wl_fixed_t x, wl_fixed_t y,
1344 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001345{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001347
Jason Ekstranda7af7042013-10-12 22:38:11 -05001348 weston_view_from_global_float(view,
1349 wl_fixed_to_double(x),
1350 wl_fixed_to_double(y),
1351 &vxf, &vyf);
1352 *vx = wl_fixed_from_double(vxf);
1353 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001354}
1355
1356WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357weston_view_from_global(struct weston_view *view,
1358 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001359{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001361
Jason Ekstranda7af7042013-10-12 22:38:11 -05001362 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1363 *vx = floorf(vxf);
1364 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001365}
1366
Bryce Harrington3f650b82015-12-23 11:01:58 -08001367/**
1368 * \param surface The surface to be repainted
1369 *
1370 * Marks the output(s) that the surface is shown on as needing to be
1371 * repainted. See weston_output_schedule_repaint().
1372 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001373WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001374weston_surface_schedule_repaint(struct weston_surface *surface)
1375{
1376 struct weston_output *output;
1377
1378 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001379 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001380 weston_output_schedule_repaint(output);
1381}
1382
Bryce Harrington3f650b82015-12-23 11:01:58 -08001383/**
1384 * \param view The view to be repainted
1385 *
1386 * Marks the output(s) that the view is shown on as needing to be
1387 * repainted. See weston_output_schedule_repaint().
1388 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001389WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390weston_view_schedule_repaint(struct weston_view *view)
1391{
1392 struct weston_output *output;
1393
1394 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001395 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 weston_output_schedule_repaint(output);
1397}
1398
Pekka Paalanene508ce62015-02-19 13:59:55 +02001399/**
1400 * XXX: This function does it the wrong way.
1401 * surface->damage is the damage from the client, and causes
1402 * surface_flush_damage() to copy pixels. No window management action can
1403 * cause damage to the client-provided content, warranting re-upload!
1404 *
1405 * Instead of surface->damage, this function should record the damage
1406 * with all the views for this surface to avoid extraneous texture
1407 * uploads.
1408 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001410weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001411{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001412 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 0, 0, surface->width,
1414 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001415
Kristian Høgsberg98238702012-08-03 16:29:12 -04001416 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001417}
1418
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001419WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001421{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001422 if (view->geometry.x == x && view->geometry.y == y)
1423 return;
1424
Jason Ekstranda7af7042013-10-12 22:38:11 -05001425 view->geometry.x = x;
1426 view->geometry.y = y;
1427 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001428}
1429
Pekka Paalanen483243f2013-03-08 14:56:50 +02001430static void
1431transform_parent_handle_parent_destroy(struct wl_listener *listener,
1432 void *data)
1433{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 struct weston_view *view =
1435 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001436 geometry.parent_destroy_listener);
1437
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001439}
1440
1441WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001443 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001444{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 if (view->geometry.parent) {
1446 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1447 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001448
1449 if (!parent)
1450 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001451 }
1452
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001454
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001456 transform_parent_handle_parent_destroy;
1457 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001458 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001460 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001462 }
1463
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 weston_view_geometry_dirty(view);
1465}
1466
Pekka Paalanen380adf52015-02-16 14:39:11 +02001467/** Set a clip mask rectangle on a view
1468 *
1469 * \param view The view to set the clip mask on.
1470 * \param x Top-left corner X coordinate of the clip rectangle.
1471 * \param y Top-left corner Y coordinate of the clip rectangle.
1472 * \param width Width of the clip rectangle, non-negative.
1473 * \param height Height of the clip rectangle, non-negative.
1474 *
1475 * A shell may set a clip mask rectangle on a view. Everything outside
1476 * the rectangle is cut away for input and output purposes: it is
1477 * not drawn and cannot be hit by hit-test based input like pointer
1478 * motion or touch-downs. Everything inside the rectangle will behave
1479 * normally. Clients are unaware of clipping.
1480 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001481 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001482 * mask rectangle does not affect the view position, the view is positioned
1483 * as it would be without a clip. The clip also does not change
1484 * weston_surface::width,height.
1485 *
1486 * The clip mask rectangle is part of transformation inheritance
1487 * (weston_view_set_transform_parent()). A clip set in the root of the
1488 * transformation inheritance tree will affect all views in the tree.
1489 * A clip can be set only on the root view. Attempting to set a clip
1490 * on view that has a transformation parent will fail. Assigning a parent
1491 * to a view that has a clip set will cause the clip to be forgotten.
1492 *
1493 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1494 * on the additional transformations in the child views. These transformations
1495 * may not rotate the coordinate axes, i.e., only translation and scaling
1496 * are allowed. Violating this restriction causes the clipping to malfunction.
1497 * Furthermore, using scaling may cause rounding errors in child clipping.
1498 *
1499 * The clip mask rectangle is not automatically adjusted based on
1500 * wl_surface.attach dx and dy arguments.
1501 *
1502 * A clip mask rectangle can be set only if the compositor capability
1503 * WESTON_CAP_VIEW_CLIP_MASK is present.
1504 *
1505 * This function sets the clip mask rectangle and schedules a repaint for
1506 * the view.
1507 */
1508WL_EXPORT void
1509weston_view_set_mask(struct weston_view *view,
1510 int x, int y, int width, int height)
1511{
1512 struct weston_compositor *compositor = view->surface->compositor;
1513
1514 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1515 weston_log("%s not allowed without capability!\n", __func__);
1516 return;
1517 }
1518
1519 if (view->geometry.parent) {
1520 weston_log("view %p has a parent, clip forbidden!\n", view);
1521 return;
1522 }
1523
1524 if (width < 0 || height < 0) {
1525 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1526 x, y, width, height);
1527 return;
1528 }
1529
1530 pixman_region32_fini(&view->geometry.scissor);
1531 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1532 view->geometry.scissor_enabled = true;
1533 weston_view_geometry_dirty(view);
1534 weston_view_schedule_repaint(view);
1535}
1536
1537/** Remove the clip mask from a view
1538 *
1539 * \param view The view to remove the clip mask from.
1540 *
1541 * Removed the clip mask rectangle and schedules a repaint.
1542 *
1543 * \sa weston_view_set_mask
1544 */
1545WL_EXPORT void
1546weston_view_set_mask_infinite(struct weston_view *view)
1547{
1548 view->geometry.scissor_enabled = false;
1549 weston_view_geometry_dirty(view);
1550 weston_view_schedule_repaint(view);
1551}
1552
Armin Krezović0da12b82016-06-30 06:04:33 +02001553/* Check if view should be displayed
1554 *
1555 * The indicator is set manually when assigning
1556 * a view to a surface.
1557 *
1558 * This needs reworking. See the thread starting at:
1559 *
1560 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1561 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001562WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563weston_view_is_mapped(struct weston_view *view)
1564{
Armin Krezović0da12b82016-06-30 06:04:33 +02001565 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001566}
1567
Armin Krezović0da12b82016-06-30 06:04:33 +02001568/* Check if a surface has a view assigned to it
1569 *
1570 * The indicator is set manually when mapping
1571 * a surface and creating a view for it.
1572 *
1573 * This needs to go. See the thread starting at:
1574 *
1575 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1576 *
1577 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001578WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001579weston_surface_is_mapped(struct weston_surface *surface)
1580{
Armin Krezović0da12b82016-06-30 06:04:33 +02001581 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001582}
1583
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001584static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001585surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001586{
1587 struct weston_view *view;
1588
1589 if (surface->width == width && surface->height == height)
1590 return;
1591
1592 surface->width = width;
1593 surface->height = height;
1594
1595 wl_list_for_each(view, &surface->views, surface_link)
1596 weston_view_geometry_dirty(view);
1597}
1598
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001599WL_EXPORT void
1600weston_surface_set_size(struct weston_surface *surface,
1601 int32_t width, int32_t height)
1602{
1603 assert(!surface->resource);
1604 surface_set_size(surface, width, height);
1605}
1606
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001607static int
1608fixed_round_up_to_int(wl_fixed_t f)
1609{
1610 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1611}
1612
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001613static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001614convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1615 int32_t width, int32_t height,
1616 uint32_t transform,
1617 int32_t scale)
1618{
1619 assert(scale > 0);
1620
1621 switch (transform) {
1622 case WL_OUTPUT_TRANSFORM_NORMAL:
1623 case WL_OUTPUT_TRANSFORM_180:
1624 case WL_OUTPUT_TRANSFORM_FLIPPED:
1625 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1626 *width_out = width / scale;
1627 *height_out = height / scale;
1628 break;
1629 case WL_OUTPUT_TRANSFORM_90:
1630 case WL_OUTPUT_TRANSFORM_270:
1631 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1632 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1633 *width_out = height / scale;
1634 *height_out = width / scale;
1635 break;
1636 default:
1637 assert(0 && "invalid transform");
1638 }
1639}
1640
1641static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001642weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001643{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001644 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001645
1646 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001647 surface->width_from_buffer = 0;
1648 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001649 return;
1650 }
1651
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001652 convert_size_by_transform_scale(&surface->width_from_buffer,
1653 &surface->height_from_buffer,
1654 surface->buffer_ref.buffer->width,
1655 surface->buffer_ref.buffer->height,
1656 vp->buffer.transform,
1657 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001658}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001659
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001660static void
1661weston_surface_update_size(struct weston_surface *surface)
1662{
1663 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1664 int32_t width, height;
1665
1666 width = surface->width_from_buffer;
1667 height = surface->height_from_buffer;
1668
1669 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001670 surface_set_size(surface,
1671 vp->surface.width, vp->surface.height);
1672 return;
1673 }
1674
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001675 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001676 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1677 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1678
1679 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001680 return;
1681 }
1682
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001683 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001684}
1685
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001686WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001687weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001688{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001689 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001690
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001691 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001692
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001693 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001694}
1695
Jason Ekstranda7af7042013-10-12 22:38:11 -05001696WL_EXPORT struct weston_view *
1697weston_compositor_pick_view(struct weston_compositor *compositor,
1698 wl_fixed_t x, wl_fixed_t y,
1699 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001700{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001701 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001702 wl_fixed_t view_x, view_y;
1703 int view_ix, view_iy;
1704 int ix = wl_fixed_to_int(x);
1705 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001706
Jason Ekstranda7af7042013-10-12 22:38:11 -05001707 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001708 if (!pixman_region32_contains_point(
1709 &view->transform.boundingbox, ix, iy, NULL))
1710 continue;
1711
1712 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1713 view_ix = wl_fixed_to_int(view_x);
1714 view_iy = wl_fixed_to_int(view_y);
1715
1716 if (!pixman_region32_contains_point(&view->surface->input,
1717 view_ix, view_iy, NULL))
1718 continue;
1719
Pekka Paalanen380adf52015-02-16 14:39:11 +02001720 if (view->geometry.scissor_enabled &&
1721 !pixman_region32_contains_point(&view->geometry.scissor,
1722 view_ix, view_iy, NULL))
1723 continue;
1724
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001725 *vx = view_x;
1726 *vy = view_y;
1727 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001728 }
1729
Derek Foremanf9318d12015-05-11 15:40:11 -05001730 *vx = wl_fixed_from_int(-1000000);
1731 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001732 return NULL;
1733}
1734
1735static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001736weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001737{
Daniel Stone37816df2012-05-16 18:45:18 +01001738 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001739
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001740 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001741 return;
1742
Daniel Stone37816df2012-05-16 18:45:18 +01001743 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001744 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001745}
1746
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001747WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001748weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001749{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001750 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001751
Jason Ekstranda7af7042013-10-12 22:38:11 -05001752 if (!weston_view_is_mapped(view))
1753 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001754
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755 weston_view_damage_below(view);
1756 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001757 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001758 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001759 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001760 wl_list_remove(&view->link);
1761 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762 view->output_mask = 0;
1763 weston_surface_assign_output(view->surface);
1764
1765 if (weston_surface_is_mapped(view->surface))
1766 return;
1767
1768 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001769 struct weston_touch *touch = weston_seat_get_touch(seat);
1770 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1771 struct weston_keyboard *keyboard =
1772 weston_seat_get_keyboard(seat);
1773
1774 if (keyboard && keyboard->focus == view->surface)
1775 weston_keyboard_set_focus(keyboard, NULL);
1776 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001777 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001778 if (touch && touch->focus == view)
1779 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001780 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001781}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001782
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783WL_EXPORT void
1784weston_surface_unmap(struct weston_surface *surface)
1785{
1786 struct weston_view *view;
1787
Armin Krezovićf8486c32016-06-30 06:04:28 +02001788 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789 wl_list_for_each(view, &surface->views, surface_link)
1790 weston_view_unmap(view);
1791 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001792}
1793
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001794static void
1795weston_surface_reset_pending_buffer(struct weston_surface *surface)
1796{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001797 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001798 surface->pending.sx = 0;
1799 surface->pending.sy = 0;
1800 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001801 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001802}
1803
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804WL_EXPORT void
1805weston_view_destroy(struct weston_view *view)
1806{
1807 wl_signal_emit(&view->destroy_signal, view);
1808
1809 assert(wl_list_empty(&view->geometry.child_list));
1810
1811 if (weston_view_is_mapped(view)) {
1812 weston_view_unmap(view);
1813 weston_compositor_build_view_list(view->surface->compositor);
1814 }
1815
1816 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001817 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818
1819 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001820 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001822 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001823
1824 weston_view_set_transform_parent(view, NULL);
1825
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826 wl_list_remove(&view->surface_link);
1827
1828 free(view);
1829}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001830
1831WL_EXPORT void
1832weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001833{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001834 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001836 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001837
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001838 if (--surface->ref_count > 0)
1839 return;
1840
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001841 assert(surface->resource == NULL);
1842
Pekka Paalanenca790762015-04-17 14:23:38 +03001843 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001844
Pekka Paalanene67858b2013-04-25 13:57:42 +03001845 assert(wl_list_empty(&surface->subsurface_list_pending));
1846 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001847
Jason Ekstranda7af7042013-10-12 22:38:11 -05001848 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1849 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001850
Jason Ekstrand7b982072014-05-20 14:33:03 -05001851 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001852
Pekka Paalanende685b82012-12-04 15:58:12 +02001853 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001854
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001855 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001856 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001857 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001858
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001859 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001860 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001861
Pekka Paalanen133e4392014-09-23 22:08:46 -04001862 weston_presentation_feedback_discard_list(&surface->feedback_list);
1863
Jonas Ådahld3414f22016-07-22 17:56:31 +08001864 wl_list_for_each_safe(constraint, next_constraint,
1865 &surface->pointer_constraints,
1866 link)
1867 weston_pointer_constraint_destroy(constraint);
1868
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001869 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001870}
1871
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001872static void
1873destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001874{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001875 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001876
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001877 assert(surface);
1878
Giulio Camuffo0d379742013-11-15 22:06:15 +01001879 /* Set the resource to NULL, since we don't want to leave a
1880 * dangling pointer if the surface was refcounted and survives
1881 * the weston_surface_destroy() call. */
1882 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001883
1884 if (surface->viewport_resource)
1885 wl_resource_set_user_data(surface->viewport_resource, NULL);
1886
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001887 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001888}
1889
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001890static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001891weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1892{
1893 struct weston_buffer *buffer =
1894 container_of(listener, struct weston_buffer, destroy_listener);
1895
1896 wl_signal_emit(&buffer->destroy_signal, buffer);
1897 free(buffer);
1898}
1899
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001900WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001901weston_buffer_from_resource(struct wl_resource *resource)
1902{
1903 struct weston_buffer *buffer;
1904 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001905
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001906 listener = wl_resource_get_destroy_listener(resource,
1907 weston_buffer_destroy_handler);
1908
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001909 if (listener)
1910 return container_of(listener, struct weston_buffer,
1911 destroy_listener);
1912
1913 buffer = zalloc(sizeof *buffer);
1914 if (buffer == NULL)
1915 return NULL;
1916
1917 buffer->resource = resource;
1918 wl_signal_init(&buffer->destroy_signal);
1919 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001920 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001921 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001922
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001923 return buffer;
1924}
1925
1926static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001927weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1928 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001929{
Pekka Paalanende685b82012-12-04 15:58:12 +02001930 struct weston_buffer_reference *ref =
1931 container_of(listener, struct weston_buffer_reference,
1932 destroy_listener);
1933
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001934 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001935 ref->buffer = NULL;
1936}
1937
1938WL_EXPORT void
1939weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001940 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001941{
1942 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001943 ref->buffer->busy_count--;
1944 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001945 assert(wl_resource_get_client(ref->buffer->resource));
1946 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001947 WL_BUFFER_RELEASE);
1948 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001949 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001950 }
1951
Pekka Paalanende685b82012-12-04 15:58:12 +02001952 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001953 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001954 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001955 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001956 }
1957
Pekka Paalanende685b82012-12-04 15:58:12 +02001958 ref->buffer = buffer;
1959 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1960}
1961
1962static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001963weston_surface_attach(struct weston_surface *surface,
1964 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001965{
1966 weston_buffer_reference(&surface->buffer_ref, buffer);
1967
Pekka Paalanena6421c42012-12-04 15:58:10 +02001968 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001969 if (weston_surface_is_mapped(surface))
1970 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001971 }
1972
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001973 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001974
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001975 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001976 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001977}
1978
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001979WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001980weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001981{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001982 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001983
1984 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001985 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001986}
1987
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001988WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001989weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001990{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001991 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001992
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001993 pixman_region32_union(&compositor->primary_plane.damage,
1994 &compositor->primary_plane.damage,
1995 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001996 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001997}
1998
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001999static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002000surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002001{
Pekka Paalanende685b82012-12-04 15:58:12 +02002002 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002003 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002004 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002005
Pekka Paalanenb5026542014-11-12 15:09:24 +02002006 if (weston_timeline_enabled_ &&
2007 pixman_region32_not_empty(&surface->damage))
2008 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2009 TLP_OUTPUT(surface->output), TLP_END);
2010
Jason Ekstrandef540082014-06-26 10:37:36 -07002011 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002012}
2013
2014static void
2015view_accumulate_damage(struct weston_view *view,
2016 pixman_region32_t *opaque)
2017{
2018 pixman_region32_t damage;
2019
2020 pixman_region32_init(&damage);
2021 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002022 pixman_box32_t *extents;
2023
Jason Ekstranda7af7042013-10-12 22:38:11 -05002024 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002025 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002026 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002027 pixman_region32_copy(&damage, &view->surface->damage);
2028 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002029 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002030 }
2031
Pekka Paalanen380adf52015-02-16 14:39:11 +02002032 pixman_region32_intersect(&damage, &damage,
2033 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002034 pixman_region32_subtract(&damage, &damage, opaque);
2035 pixman_region32_union(&view->plane->damage,
2036 &view->plane->damage, &damage);
2037 pixman_region32_fini(&damage);
2038 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002039 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002040}
2041
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002042static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002043compositor_accumulate_damage(struct weston_compositor *ec)
2044{
2045 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002047 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002048
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002049 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002050
2051 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002052 pixman_region32_copy(&plane->clip, &clip);
2053
2054 pixman_region32_init(&opaque);
2055
Jason Ekstranda7af7042013-10-12 22:38:11 -05002056 wl_list_for_each(ev, &ec->view_list, link) {
2057 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002058 continue;
2059
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002061 }
2062
2063 pixman_region32_union(&clip, &clip, &opaque);
2064 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002065 }
2066
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002067 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002068
Jason Ekstranda7af7042013-10-12 22:38:11 -05002069 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002070 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002071
2072 wl_list_for_each(ev, &ec->view_list, link) {
2073 if (ev->surface->touched)
2074 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002075 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076
2077 surface_flush_damage(ev->surface);
2078
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002079 /* Both the renderer and the backend have seen the buffer
2080 * by now. If renderer needs the buffer, it has its own
2081 * reference set. If the backend wants to keep the buffer
2082 * around for migrating the surface into a non-primary plane
2083 * later, keep_buffer is true. Otherwise, drop the core
2084 * reference now, and allow early buffer release. This enables
2085 * clients to use single-buffering.
2086 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087 if (!ev->surface->keep_buffer)
2088 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002089 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002090}
2091
2092static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002094{
2095 struct weston_subsurface *sub;
2096
Pekka Paalanene67858b2013-04-25 13:57:42 +03002097 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002098 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002099 continue;
2100
Jason Ekstranda7af7042013-10-12 22:38:11 -05002101 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2102 wl_list_init(&sub->surface->views);
2103
2104 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002105 }
2106}
2107
2108static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002110{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002111 struct weston_subsurface *sub;
2112 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002113
Jason Ekstranda7af7042013-10-12 22:38:11 -05002114 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2115 if (sub->surface == surface)
2116 continue;
2117
George Kiagiadakised04d382014-06-13 18:10:26 +02002118 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2119 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002120 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002121 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002122
2123 surface_free_unused_subsurface_views(sub->surface);
2124 }
2125}
2126
2127static void
2128view_list_add_subsurface_view(struct weston_compositor *compositor,
2129 struct weston_subsurface *sub,
2130 struct weston_view *parent)
2131{
2132 struct weston_subsurface *child;
2133 struct weston_view *view = NULL, *iv;
2134
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002135 if (!weston_surface_is_mapped(sub->surface))
2136 return;
2137
Jason Ekstranda7af7042013-10-12 22:38:11 -05002138 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2139 if (iv->geometry.parent == parent) {
2140 view = iv;
2141 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002142 }
2143 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002144
2145 if (view) {
2146 /* Put it back in the surface's list of views */
2147 wl_list_remove(&view->surface_link);
2148 wl_list_insert(&sub->surface->views, &view->surface_link);
2149 } else {
2150 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002151 weston_view_set_position(view,
2152 sub->position.x,
2153 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002154 weston_view_set_transform_parent(view, parent);
2155 }
2156
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002157 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002158 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002159 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002160
Pekka Paalanenb188e912013-11-19 14:03:35 +02002161 if (wl_list_empty(&sub->surface->subsurface_list)) {
2162 wl_list_insert(compositor->view_list.prev, &view->link);
2163 return;
2164 }
2165
2166 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2167 if (child->surface == sub->surface)
2168 wl_list_insert(compositor->view_list.prev, &view->link);
2169 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002170 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002171 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172}
2173
2174static void
2175view_list_add(struct weston_compositor *compositor,
2176 struct weston_view *view)
2177{
2178 struct weston_subsurface *sub;
2179
2180 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181
Pekka Paalanenb188e912013-11-19 14:03:35 +02002182 if (wl_list_empty(&view->surface->subsurface_list)) {
2183 wl_list_insert(compositor->view_list.prev, &view->link);
2184 return;
2185 }
2186
2187 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2188 if (sub->surface == view->surface)
2189 wl_list_insert(compositor->view_list.prev, &view->link);
2190 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002192 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002193}
2194
2195static void
2196weston_compositor_build_view_list(struct weston_compositor *compositor)
2197{
2198 struct weston_view *view;
2199 struct weston_layer *layer;
2200
2201 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002202 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 surface_stash_subsurface_views(view->surface);
2204
2205 wl_list_init(&compositor->view_list);
2206 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002207 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002208 view_list_add(compositor, view);
2209 }
2210 }
2211
2212 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002213 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002215}
2216
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002217static void
2218weston_output_take_feedback_list(struct weston_output *output,
2219 struct weston_surface *surface)
2220{
2221 struct weston_view *view;
2222 struct weston_presentation_feedback *feedback;
2223 uint32_t flags = 0xffffffff;
2224
2225 if (wl_list_empty(&surface->feedback_list))
2226 return;
2227
2228 /* All views must have the flag for the flag to survive. */
2229 wl_list_for_each(view, &surface->views, surface_link) {
2230 /* ignore views that are not on this output at all */
2231 if (view->output_mask & (1u << output->id))
2232 flags &= view->psf_flags;
2233 }
2234
2235 wl_list_for_each(feedback, &surface->feedback_list, link)
2236 feedback->psf_flags = flags;
2237
2238 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2239 wl_list_init(&surface->feedback_list);
2240}
2241
David Herrmann1edf44c2013-10-22 17:11:26 +02002242static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002243weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002244{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002245 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002246 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002247 struct weston_animation *animation, *next;
2248 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002249 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002250 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002251 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002252
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002253 if (output->destroying)
2254 return 0;
2255
Pekka Paalanenb5026542014-11-12 15:09:24 +02002256 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2257
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002258 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002259 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002260
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002261 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002262 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002263 } else {
2264 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002265 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002266 ev->psf_flags = 0;
2267 }
2268 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002269
Pekka Paalanene67858b2013-04-25 13:57:42 +03002270 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002271 wl_list_for_each(ev, &ec->view_list, link) {
2272 /* Note: This operation is safe to do multiple times on the
2273 * same surface.
2274 */
2275 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002276 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002277 &ev->surface->frame_callback_list);
2278 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002279
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002280 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002281 }
2282 }
2283
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002284 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002285
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002286 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002287 pixman_region32_intersect(&output_damage,
2288 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002289 pixman_region32_subtract(&output_damage,
2290 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002291
Scott Moreauccbf29d2012-02-22 14:21:41 -07002292 if (output->dirty)
2293 weston_output_update_matrix(output);
2294
David Herrmann1edf44c2013-10-22 17:11:26 +02002295 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002296
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002297 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002298
Kristian Høgsbergef044142011-06-21 15:02:12 -04002299 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002300
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002301 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002302
Jonas Ådahldb773762012-06-13 00:01:21 +02002303 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002304 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002305 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002306 }
2307
Scott Moreaud64cf212012-06-08 19:40:54 -06002308 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002309 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002310 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002311 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002312
Pekka Paalanenb5026542014-11-12 15:09:24 +02002313 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2314
David Herrmann1edf44c2013-10-22 17:11:26 +02002315 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002316}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002317
Pekka Paalanen82919792014-05-21 13:51:49 +03002318static void
2319weston_output_schedule_repaint_reset(struct weston_output *output)
2320{
Pekka Paalanen82919792014-05-21 13:51:49 +03002321 output->repaint_scheduled = 0;
2322 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002323}
2324
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002325/** Retrieves a mask of outputs that should inhibit screensaving
2326 *
2327 * \param compositor The compositor instance.
2328 * \return An output mask indicating the ids of all inhibiting outputs
2329 *
2330 * Checks for surfaces whose clients have requested that they
2331 * disable the screenserver and display powersaving. Note
2332 * the output ids for these surfaces.
2333 */
2334WL_EXPORT uint32_t
2335weston_compositor_inhibited_outputs(struct weston_compositor *compositor)
2336{
2337 struct weston_view *view;
2338 uint32_t inhibited_outputs_mask = 0;
2339
2340 wl_list_for_each(view, &compositor->view_list, link) {
2341 /* Does the view's surface inhibit this output? */
2342 if (!view->surface->inhibit_idling)
2343 continue;
2344
2345 inhibited_outputs_mask |= view->output_mask;
2346 }
2347 return inhibited_outputs_mask;
2348}
2349
Pekka Paalanen0513a952014-05-21 16:17:27 +03002350static int
2351output_repaint_timer_handler(void *data)
2352{
2353 struct weston_output *output = data;
2354 struct weston_compositor *compositor = output->compositor;
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002355 uint32_t inhibited_outputs_mask = weston_compositor_inhibited_outputs(compositor);
Pekka Paalanen0513a952014-05-21 16:17:27 +03002356
2357 if (output->repaint_needed &&
Pekka Paalanen0513a952014-05-21 16:17:27 +03002358 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002359 (compositor->state != WESTON_COMPOSITOR_SLEEPING
2360 || inhibited_outputs_mask & (1 << output->id)) &&
Pekka Paalanen0513a952014-05-21 16:17:27 +03002361 weston_output_repaint(output) == 0)
2362 return 0;
2363
2364 weston_output_schedule_repaint_reset(output);
2365
2366 return 0;
2367}
2368
Kristian Høgsbergef044142011-06-21 15:02:12 -04002369WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002370weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002371 const struct timespec *stamp,
2372 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002373{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002374 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002375 int32_t refresh_nsec;
2376 struct timespec now;
2377 struct timespec gone;
2378 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002379
Pekka Paalanenb5026542014-11-12 15:09:24 +02002380 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2381 TLP_VBLANK(stamp), TLP_END);
2382
Pekka Paalanend7894d02015-07-03 15:08:53 +03002383 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002384 weston_presentation_feedback_present_list(&output->feedback_list,
2385 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002386 output->msc,
2387 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002388
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002389 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002390
Pekka Paalanen0513a952014-05-21 16:17:27 +03002391 weston_compositor_read_presentation_clock(compositor, &now);
2392 timespec_sub(&gone, &now, stamp);
2393 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2394 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002395
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002396 if (msec < -1000 || msec > 1000) {
2397 static bool warned;
2398
2399 if (!warned)
2400 weston_log("Warning: computed repaint delay is "
2401 "insane: %d msec\n", msec);
2402 warned = true;
2403
2404 msec = 0;
2405 }
2406
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002407 /* Called from restart_repaint_loop and restart happens already after
2408 * the deadline given by repaint_msec? In that case we delay until
2409 * the deadline of the next frame, to give clients a more predictable
2410 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002411 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002412 msec += refresh_nsec / 1000000;
2413
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002414 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002415 output_repaint_timer_handler(output);
2416 else
2417 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002418}
2419
2420static void
2421idle_repaint(void *data)
2422{
2423 struct weston_output *output = data;
2424
Jonas Ådahle5a12252013-04-05 23:07:11 +02002425 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002426}
2427
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002428WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002429weston_layer_entry_insert(struct weston_layer_entry *list,
2430 struct weston_layer_entry *entry)
2431{
2432 wl_list_insert(&list->link, &entry->link);
2433 entry->layer = list->layer;
2434}
2435
2436WL_EXPORT void
2437weston_layer_entry_remove(struct weston_layer_entry *entry)
2438{
2439 wl_list_remove(&entry->link);
2440 wl_list_init(&entry->link);
2441 entry->layer = NULL;
2442}
2443
2444WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002445weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2446{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002447 wl_list_init(&layer->view_list.link);
2448 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002449 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002450 if (below != NULL)
2451 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002452}
2453
2454WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002455weston_layer_set_mask(struct weston_layer *layer,
2456 int x, int y, int width, int height)
2457{
2458 struct weston_view *view;
2459
2460 layer->mask.x1 = x;
2461 layer->mask.x2 = x + width;
2462 layer->mask.y1 = y;
2463 layer->mask.y2 = y + height;
2464
2465 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2466 weston_view_geometry_dirty(view);
2467 }
2468}
2469
2470WL_EXPORT void
2471weston_layer_set_mask_infinite(struct weston_layer *layer)
2472{
2473 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2474 UINT32_MAX, UINT32_MAX);
2475}
2476
2477WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002478weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002479{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002480 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002481 struct wl_event_loop *loop;
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002482 uint32_t inhibited_outputs_mask = weston_compositor_inhibited_outputs(compositor);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002483
Bryce Harringtonf8300c82016-08-10 17:25:22 -07002484 /* If we're offscreen, or if we're sleeping and the monitor
2485 * isn't currently being inhibited by an active surface, then
2486 * skip repainting.
2487 */
2488 if (compositor->state == WESTON_COMPOSITOR_OFFSCREEN ||
2489 (compositor->state == WESTON_COMPOSITOR_SLEEPING &&
2490 !(inhibited_outputs_mask & (1 << output->id))))
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002491 return;
2492
Pekka Paalanenb5026542014-11-12 15:09:24 +02002493 if (!output->repaint_needed)
2494 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2495
Kristian Høgsbergef044142011-06-21 15:02:12 -04002496 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002497 output->repaint_needed = 1;
2498 if (output->repaint_scheduled)
2499 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002500
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002501 wl_event_loop_add_idle(loop, idle_repaint, output);
2502 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002503 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002504}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002505
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002506WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002507weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2508{
2509 struct weston_output *output;
2510
2511 wl_list_for_each(output, &compositor->output_list, link)
2512 weston_output_schedule_repaint(output);
2513}
2514
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002515static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002516surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002517{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002518 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002519}
2520
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002521static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002522surface_attach(struct wl_client *client,
2523 struct wl_resource *resource,
2524 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2525{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002526 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002527 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002528
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002529 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002530 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002531 if (buffer == NULL) {
2532 wl_client_post_no_memory(client);
2533 return;
2534 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002535 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002536
Pekka Paalanende685b82012-12-04 15:58:12 +02002537 /* Attach, attach, without commit in between does not send
2538 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002539 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002540
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002541 surface->pending.sx = sx;
2542 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002543 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002544}
2545
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002546static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002547surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002548 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002549 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002550{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002551 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002552
Derek Foreman57e92ed2015-11-17 14:11:35 -06002553 if (width <= 0 || height <= 0)
2554 return;
2555
Derek Foreman152254b2015-11-26 14:17:48 -06002556 pixman_region32_union_rect(&surface->pending.damage_surface,
2557 &surface->pending.damage_surface,
2558 x, y, width, height);
2559}
2560
2561static void
2562surface_damage_buffer(struct wl_client *client,
2563 struct wl_resource *resource,
2564 int32_t x, int32_t y, int32_t width, int32_t height)
2565{
2566 struct weston_surface *surface = wl_resource_get_user_data(resource);
2567
2568 if (width <= 0 || height <= 0)
2569 return;
2570
2571 pixman_region32_union_rect(&surface->pending.damage_buffer,
2572 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002573 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002574}
2575
Kristian Høgsberg33418202011-08-16 23:01:28 -04002576static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002577destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002578{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002579 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002580
2581 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002582 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002583}
2584
2585static void
2586surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002587 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002588{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002589 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002590 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002591
2592 cb = malloc(sizeof *cb);
2593 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002594 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002595 return;
2596 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002597
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002598 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2599 callback);
2600 if (cb->resource == NULL) {
2601 free(cb);
2602 wl_resource_post_no_memory(resource);
2603 return;
2604 }
2605
Jason Ekstranda85118c2013-06-27 20:17:02 -05002606 wl_resource_set_implementation(cb->resource, NULL, cb,
2607 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002608
Pekka Paalanenbc106382012-10-10 12:49:31 +03002609 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002610}
2611
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002612static void
2613surface_set_opaque_region(struct wl_client *client,
2614 struct wl_resource *resource,
2615 struct wl_resource *region_resource)
2616{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002617 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002618 struct weston_region *region;
2619
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002620 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002621 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002622 pixman_region32_copy(&surface->pending.opaque,
2623 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002624 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002625 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002626 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002627}
2628
2629static void
2630surface_set_input_region(struct wl_client *client,
2631 struct wl_resource *resource,
2632 struct wl_resource *region_resource)
2633{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002634 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002635 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002636
2637 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002638 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002639 pixman_region32_copy(&surface->pending.input,
2640 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002641 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002642 pixman_region32_fini(&surface->pending.input);
2643 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002644 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002645}
2646
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002647static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002648weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002649{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002650 struct weston_subsurface *sub;
2651
2652 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2653 parent_link_pending) {
2654 wl_list_remove(&sub->parent_link);
2655 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2656 }
2657}
2658
2659static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002660weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002661 struct weston_matrix *matrix)
2662{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002663 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002664 double src_width, src_height, dest_width, dest_height;
2665
2666 weston_matrix_init(matrix);
2667
2668 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2669 src_width = surface->width_from_buffer;
2670 src_height = surface->height_from_buffer;
2671 } else {
2672 src_width = wl_fixed_to_double(vp->buffer.src_width);
2673 src_height = wl_fixed_to_double(vp->buffer.src_height);
2674 }
2675
2676 if (vp->surface.width == -1) {
2677 dest_width = src_width;
2678 dest_height = src_height;
2679 } else {
2680 dest_width = vp->surface.width;
2681 dest_height = vp->surface.height;
2682 }
2683
2684 if (src_width != dest_width || src_height != dest_height)
2685 weston_matrix_scale(matrix,
2686 src_width / dest_width,
2687 src_height / dest_height, 1);
2688
2689 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2690 weston_matrix_translate(matrix,
2691 wl_fixed_to_double(vp->buffer.src_x),
2692 wl_fixed_to_double(vp->buffer.src_y),
2693 0);
2694
2695 switch (vp->buffer.transform) {
2696 case WL_OUTPUT_TRANSFORM_FLIPPED:
2697 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2698 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2699 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2700 weston_matrix_scale(matrix, -1, 1, 1);
2701 weston_matrix_translate(matrix,
2702 surface->width_from_buffer, 0, 0);
2703 break;
2704 }
2705
2706 switch (vp->buffer.transform) {
2707 default:
2708 case WL_OUTPUT_TRANSFORM_NORMAL:
2709 case WL_OUTPUT_TRANSFORM_FLIPPED:
2710 break;
2711 case WL_OUTPUT_TRANSFORM_90:
2712 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2713 weston_matrix_rotate_xy(matrix, 0, 1);
2714 weston_matrix_translate(matrix,
2715 surface->height_from_buffer, 0, 0);
2716 break;
2717 case WL_OUTPUT_TRANSFORM_180:
2718 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2719 weston_matrix_rotate_xy(matrix, -1, 0);
2720 weston_matrix_translate(matrix,
2721 surface->width_from_buffer,
2722 surface->height_from_buffer, 0);
2723 break;
2724 case WL_OUTPUT_TRANSFORM_270:
2725 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2726 weston_matrix_rotate_xy(matrix, 0, -1);
2727 weston_matrix_translate(matrix,
2728 0, surface->width_from_buffer, 0);
2729 break;
2730 }
2731
2732 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2733}
2734
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002735/**
2736 * Compute a + b > c while being safe to overflows.
2737 */
2738static bool
2739fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2740{
2741 return (int64_t)a + (int64_t)b > (int64_t)c;
2742}
2743
2744static bool
2745weston_surface_is_pending_viewport_source_valid(
2746 const struct weston_surface *surface)
2747{
2748 const struct weston_surface_state *pend = &surface->pending;
2749 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2750 int width_from_buffer = 0;
2751 int height_from_buffer = 0;
2752 wl_fixed_t w;
2753 wl_fixed_t h;
2754
2755 /* If viewport source rect is not set, it is always ok. */
2756 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2757 return true;
2758
2759 if (pend->newly_attached) {
2760 if (pend->buffer) {
2761 convert_size_by_transform_scale(&width_from_buffer,
2762 &height_from_buffer,
2763 pend->buffer->width,
2764 pend->buffer->height,
2765 vp->buffer.transform,
2766 vp->buffer.scale);
2767 } else {
2768 /* No buffer: viewport is irrelevant. */
2769 return true;
2770 }
2771 } else {
2772 width_from_buffer = surface->width_from_buffer;
2773 height_from_buffer = surface->height_from_buffer;
2774 }
2775
2776 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2777 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2778
2779 /* No buffer: viewport is irrelevant. */
2780 if (width_from_buffer == 0 || height_from_buffer == 0)
2781 return true;
2782
2783 /* overflow checks for wl_fixed_from_int() */
2784 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2785 return false;
2786 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2787 return false;
2788
2789 w = wl_fixed_from_int(width_from_buffer);
2790 h = wl_fixed_from_int(height_from_buffer);
2791
2792 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2793 return false;
2794 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2795 return false;
2796
2797 return true;
2798}
2799
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002800static bool
2801fixed_is_integer(wl_fixed_t v)
2802{
2803 return (v & 0xff) == 0;
2804}
2805
2806static bool
2807weston_surface_is_pending_viewport_dst_size_int(
2808 const struct weston_surface *surface)
2809{
2810 const struct weston_buffer_viewport *vp =
2811 &surface->pending.buffer_viewport;
2812
2813 if (vp->surface.width != -1) {
2814 assert(vp->surface.width > 0 && vp->surface.height > 0);
2815 return true;
2816 }
2817
2818 return fixed_is_integer(vp->buffer.src_width) &&
2819 fixed_is_integer(vp->buffer.src_height);
2820}
2821
Derek Foreman152254b2015-11-26 14:17:48 -06002822/* Translate pending damage in buffer co-ordinates to surface
2823 * co-ordinates and union it with a pixman_region32_t.
2824 * This should only be called after the buffer is attached.
2825 */
2826static void
2827apply_damage_buffer(pixman_region32_t *dest,
2828 struct weston_surface *surface,
2829 struct weston_surface_state *state)
2830{
2831 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2832
2833 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2834 * translated into surface co-ordinates and unioned with
2835 * any other surface damage.
2836 * None of this makes sense if there is no buffer though.
2837 */
2838 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2839 pixman_region32_t buffer_damage;
2840
2841 pixman_region32_intersect_rect(&state->damage_buffer,
2842 &state->damage_buffer,
2843 0, 0, buffer->width,
2844 buffer->height);
2845 pixman_region32_init(&buffer_damage);
2846 weston_matrix_transform_region(&buffer_damage,
2847 &surface->buffer_to_surface_matrix,
2848 &state->damage_buffer);
2849 pixman_region32_union(dest, dest, &buffer_damage);
2850 pixman_region32_fini(&buffer_damage);
2851 }
2852 /* We should clear this on commit even if there was no buffer */
2853 pixman_region32_clear(&state->damage_buffer);
2854}
2855
Jason Ekstrand1e059042014-10-16 10:55:19 -05002856static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002857weston_surface_commit_state(struct weston_surface *surface,
2858 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002859{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002860 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002861 pixman_region32_t opaque;
2862
Alexander Larsson4ea95522013-05-22 14:41:37 +02002863 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002864 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002865 /* wp_viewport.set_source */
2866 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002867 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002868
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002869 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002870 if (state->newly_attached)
2871 weston_surface_attach(surface, state->buffer);
2872 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002873
Jason Ekstrand1e059042014-10-16 10:55:19 -05002874 weston_surface_build_buffer_matrix(surface,
2875 &surface->surface_to_buffer_matrix);
2876 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2877 &surface->surface_to_buffer_matrix);
2878
Jason Ekstrand7b982072014-05-20 14:33:03 -05002879 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002880 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002881 if (surface->committed)
2882 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002883 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002884
Jason Ekstrand7b982072014-05-20 14:33:03 -05002885 state->sx = 0;
2886 state->sy = 0;
2887 state->newly_attached = 0;
2888 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002889
Derek Foreman152254b2015-11-26 14:17:48 -06002890 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002891 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002892 (pixman_region32_not_empty(&state->damage_surface) ||
2893 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002894 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002895
Pekka Paalanen8e159182012-10-10 12:49:25 +03002896 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002897 &state->damage_surface);
2898
2899 apply_damage_buffer(&surface->damage, surface, state);
2900
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002901 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002902 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002903 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002904
2905 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002906 pixman_region32_init(&opaque);
2907 pixman_region32_intersect_rect(&opaque, &state->opaque,
2908 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002909
2910 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2911 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002912 wl_list_for_each(view, &surface->views, surface_link)
2913 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002914 }
2915
2916 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002917
2918 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002919 pixman_region32_intersect_rect(&surface->input, &state->input,
2920 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002921
Pekka Paalanenbc106382012-10-10 12:49:31 +03002922 /* wl_surface.frame */
2923 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002924 &state->frame_callback_list);
2925 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002926
2927 /* XXX:
2928 * What should happen with a feedback request, if there
2929 * is no wl_buffer attached for this commit?
2930 */
2931
2932 /* presentation.feedback */
2933 wl_list_insert_list(&surface->feedback_list,
2934 &state->feedback_list);
2935 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08002936
2937 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002938}
2939
2940static void
2941weston_surface_commit(struct weston_surface *surface)
2942{
2943 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002944
Pekka Paalanene67858b2013-04-25 13:57:42 +03002945 weston_surface_commit_subsurface_order(surface);
2946
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002947 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002948}
2949
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002950static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002951weston_subsurface_commit(struct weston_subsurface *sub);
2952
2953static void
2954weston_subsurface_parent_commit(struct weston_subsurface *sub,
2955 int parent_is_synchronized);
2956
2957static void
2958surface_commit(struct wl_client *client, struct wl_resource *resource)
2959{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002960 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002961 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2962
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002963 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2964 assert(surface->viewport_resource);
2965
2966 wl_resource_post_error(surface->viewport_resource,
2967 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2968 "wl_surface@%d has viewport source outside buffer",
2969 wl_resource_get_id(resource));
2970 return;
2971 }
2972
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002973 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
2974 assert(surface->viewport_resource);
2975
2976 wl_resource_post_error(surface->viewport_resource,
2977 WP_VIEWPORT_ERROR_BAD_SIZE,
2978 "wl_surface@%d viewport dst size not integer",
2979 wl_resource_get_id(resource));
2980 return;
2981 }
2982
Pekka Paalanene67858b2013-04-25 13:57:42 +03002983 if (sub) {
2984 weston_subsurface_commit(sub);
2985 return;
2986 }
2987
2988 weston_surface_commit(surface);
2989
2990 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2991 if (sub->surface != surface)
2992 weston_subsurface_parent_commit(sub, 0);
2993 }
2994}
2995
2996static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002997surface_set_buffer_transform(struct wl_client *client,
2998 struct wl_resource *resource, int transform)
2999{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003000 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003001
Jonny Lamba55f1392014-05-30 12:07:15 +02003002 /* if wl_output.transform grows more members this will need to be updated. */
3003 if (transform < 0 ||
3004 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3005 wl_resource_post_error(resource,
3006 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3007 "buffer transform must be a valid transform "
3008 "('%d' specified)", transform);
3009 return;
3010 }
3011
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003012 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003013 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003014}
3015
Alexander Larsson4ea95522013-05-22 14:41:37 +02003016static void
3017surface_set_buffer_scale(struct wl_client *client,
3018 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003019 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003020{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003021 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003022
Jonny Lamba55f1392014-05-30 12:07:15 +02003023 if (scale < 1) {
3024 wl_resource_post_error(resource,
3025 WL_SURFACE_ERROR_INVALID_SCALE,
3026 "buffer scale must be at least one "
3027 "('%d' specified)", scale);
3028 return;
3029 }
3030
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003031 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003032 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003033}
3034
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003035static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003036 surface_destroy,
3037 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003038 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003039 surface_frame,
3040 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003041 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003042 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003043 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003044 surface_set_buffer_scale,
3045 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003046};
3047
3048static void
3049compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003050 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003051{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003052 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003053 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003054
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003055 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003056 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003057 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003058 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003059 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003060
Jason Ekstranda85118c2013-06-27 20:17:02 -05003061 surface->resource =
3062 wl_resource_create(client, &wl_surface_interface,
3063 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003064 if (surface->resource == NULL) {
3065 weston_surface_destroy(surface);
3066 wl_resource_post_no_memory(resource);
3067 return;
3068 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003069 wl_resource_set_implementation(surface->resource, &surface_interface,
3070 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003071
3072 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003073}
3074
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003075static void
3076destroy_region(struct wl_resource *resource)
3077{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003078 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003079
3080 pixman_region32_fini(&region->region);
3081 free(region);
3082}
3083
3084static void
3085region_destroy(struct wl_client *client, struct wl_resource *resource)
3086{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003087 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003088}
3089
3090static void
3091region_add(struct wl_client *client, struct wl_resource *resource,
3092 int32_t x, int32_t y, int32_t width, int32_t height)
3093{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003094 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003095
3096 pixman_region32_union_rect(&region->region, &region->region,
3097 x, y, width, height);
3098}
3099
3100static void
3101region_subtract(struct wl_client *client, struct wl_resource *resource,
3102 int32_t x, int32_t y, int32_t width, int32_t height)
3103{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003104 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003105 pixman_region32_t rect;
3106
3107 pixman_region32_init_rect(&rect, x, y, width, height);
3108 pixman_region32_subtract(&region->region, &region->region, &rect);
3109 pixman_region32_fini(&rect);
3110}
3111
3112static const struct wl_region_interface region_interface = {
3113 region_destroy,
3114 region_add,
3115 region_subtract
3116};
3117
3118static void
3119compositor_create_region(struct wl_client *client,
3120 struct wl_resource *resource, uint32_t id)
3121{
3122 struct weston_region *region;
3123
3124 region = malloc(sizeof *region);
3125 if (region == NULL) {
3126 wl_resource_post_no_memory(resource);
3127 return;
3128 }
3129
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003130 pixman_region32_init(&region->region);
3131
Jason Ekstranda85118c2013-06-27 20:17:02 -05003132 region->resource =
3133 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003134 if (region->resource == NULL) {
3135 free(region);
3136 wl_resource_post_no_memory(resource);
3137 return;
3138 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003139 wl_resource_set_implementation(region->resource, &region_interface,
3140 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003141}
3142
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003143static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003144 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003145 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003146};
3147
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003148static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003149weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3150{
3151 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003152
Jason Ekstrand7b982072014-05-20 14:33:03 -05003153 weston_surface_commit_state(surface, &sub->cached);
3154 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003155
3156 weston_surface_commit_subsurface_order(surface);
3157
3158 weston_surface_schedule_repaint(surface);
3159
Jason Ekstrand7b982072014-05-20 14:33:03 -05003160 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003161}
3162
3163static void
3164weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3165{
3166 struct weston_surface *surface = sub->surface;
3167
3168 /*
3169 * If this commit would cause the surface to move by the
3170 * attach(dx, dy) parameters, the old damage region must be
3171 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003172 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003173 */
Derek Foreman152254b2015-11-26 14:17:48 -06003174 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003175 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003176 pixman_region32_union(&sub->cached.damage_surface,
3177 &sub->cached.damage_surface,
3178 &surface->pending.damage_surface);
3179 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003180
3181 if (surface->pending.newly_attached) {
3182 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003183 weston_surface_state_set_buffer(&sub->cached,
3184 surface->pending.buffer);
3185 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003186 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003187 weston_presentation_feedback_discard_list(
3188 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189 }
3190 sub->cached.sx += surface->pending.sx;
3191 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003192
Derek Foreman152254b2015-11-26 14:17:48 -06003193 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3194
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003195 sub->cached.buffer_viewport.changed |=
3196 surface->pending.buffer_viewport.changed;
3197 sub->cached.buffer_viewport.buffer =
3198 surface->pending.buffer_viewport.buffer;
3199 sub->cached.buffer_viewport.surface =
3200 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003201
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003202 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003203
3204 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3205
3206 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3207
3208 wl_list_insert_list(&sub->cached.frame_callback_list,
3209 &surface->pending.frame_callback_list);
3210 wl_list_init(&surface->pending.frame_callback_list);
3211
Pekka Paalanen133e4392014-09-23 22:08:46 -04003212 wl_list_insert_list(&sub->cached.feedback_list,
3213 &surface->pending.feedback_list);
3214 wl_list_init(&surface->pending.feedback_list);
3215
Jason Ekstrand7b982072014-05-20 14:33:03 -05003216 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003217}
3218
Derek Foreman280e7dd2014-10-03 13:13:42 -05003219static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003220weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3221{
3222 while (sub) {
3223 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003224 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003225
3226 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003227 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003228
3229 sub = weston_surface_to_subsurface(sub->parent);
3230 }
3231
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003232 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003233}
3234
3235static void
3236weston_subsurface_commit(struct weston_subsurface *sub)
3237{
3238 struct weston_surface *surface = sub->surface;
3239 struct weston_subsurface *tmp;
3240
3241 /* Recursive check for effectively synchronized. */
3242 if (weston_subsurface_is_synchronized(sub)) {
3243 weston_subsurface_commit_to_cache(sub);
3244 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003245 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003246 /* flush accumulated state from cache */
3247 weston_subsurface_commit_to_cache(sub);
3248 weston_subsurface_commit_from_cache(sub);
3249 } else {
3250 weston_surface_commit(surface);
3251 }
3252
3253 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3254 if (tmp->surface != surface)
3255 weston_subsurface_parent_commit(tmp, 0);
3256 }
3257 }
3258}
3259
3260static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003261weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003262{
3263 struct weston_surface *surface = sub->surface;
3264 struct weston_subsurface *tmp;
3265
Pekka Paalanene67858b2013-04-25 13:57:42 +03003266 /* From now on, commit_from_cache the whole sub-tree, regardless of
3267 * the synchronized mode of each child. This sub-surface or some
3268 * of its ancestors were synchronized, so we are synchronized
3269 * all the way down.
3270 */
3271
Jason Ekstrand7b982072014-05-20 14:33:03 -05003272 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003273 weston_subsurface_commit_from_cache(sub);
3274
3275 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3276 if (tmp->surface != surface)
3277 weston_subsurface_parent_commit(tmp, 1);
3278 }
3279}
3280
3281static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003282weston_subsurface_parent_commit(struct weston_subsurface *sub,
3283 int parent_is_synchronized)
3284{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003285 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003286 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003287 wl_list_for_each(view, &sub->surface->views, surface_link)
3288 weston_view_set_position(view,
3289 sub->position.x,
3290 sub->position.y);
3291
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003292 sub->position.set = 0;
3293 }
3294
3295 if (parent_is_synchronized || sub->synchronized)
3296 weston_subsurface_synchronized_commit(sub);
3297}
3298
Pekka Paalanen8274d902014-08-06 19:36:51 +03003299static int
3300subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3301{
3302 return snprintf(buf, len, "sub-surface");
3303}
3304
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003305static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003306subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003307{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003308 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003309
Jason Ekstranda7af7042013-10-12 22:38:11 -05003310 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003311 weston_view_set_position(view,
3312 view->geometry.x + dx,
3313 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003314
3315 /* No need to check parent mappedness, because if parent is not
3316 * mapped, parent is not in a visible layer, so this sub-surface
3317 * will not be drawn either.
3318 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003319
Pekka Paalanene67858b2013-04-25 13:57:42 +03003320 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003321 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003322
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003323 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003324 * because that would call it also for the parent surface,
3325 * which might not be mapped yet. That would lead to
3326 * inconsistent state, where the window could never be
3327 * mapped.
3328 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003329 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003330 * weston_surface_is_mapped() return true, so that when the
3331 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003332 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003333 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003334 }
3335}
3336
3337static struct weston_subsurface *
3338weston_surface_to_subsurface(struct weston_surface *surface)
3339{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003340 if (surface->committed == subsurface_committed)
3341 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003342
3343 return NULL;
3344}
3345
Pekka Paalanen01388e22013-04-25 13:57:44 +03003346WL_EXPORT struct weston_surface *
3347weston_surface_get_main_surface(struct weston_surface *surface)
3348{
3349 struct weston_subsurface *sub;
3350
3351 while (surface && (sub = weston_surface_to_subsurface(surface)))
3352 surface = sub->parent;
3353
3354 return surface;
3355}
3356
Pekka Paalanen50b67472014-10-01 15:02:41 +03003357WL_EXPORT int
3358weston_surface_set_role(struct weston_surface *surface,
3359 const char *role_name,
3360 struct wl_resource *error_resource,
3361 uint32_t error_code)
3362{
3363 assert(role_name);
3364
3365 if (surface->role_name == NULL ||
3366 surface->role_name == role_name ||
3367 strcmp(surface->role_name, role_name) == 0) {
3368 surface->role_name = role_name;
3369
3370 return 0;
3371 }
3372
3373 wl_resource_post_error(error_resource, error_code,
3374 "Cannot assign role %s to wl_surface@%d,"
3375 " already has role %s\n",
3376 role_name,
3377 wl_resource_get_id(surface->resource),
3378 surface->role_name);
3379 return -1;
3380}
3381
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003382WL_EXPORT const char *
3383weston_surface_get_role(struct weston_surface *surface)
3384{
3385 return surface->role_name;
3386}
3387
Pekka Paalanen8274d902014-08-06 19:36:51 +03003388WL_EXPORT void
3389weston_surface_set_label_func(struct weston_surface *surface,
3390 int (*desc)(struct weston_surface *,
3391 char *, size_t))
3392{
3393 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003394 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003395}
3396
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003397/** Get the size of surface contents
3398 *
3399 * \param surface The surface to query.
3400 * \param width Returns the width of raw contents.
3401 * \param height Returns the height of raw contents.
3402 *
3403 * Retrieves the raw surface content size in pixels for the given surface.
3404 * This is the whole content size in buffer pixels. If the surface
3405 * has no content or the renderer does not implement this feature,
3406 * zeroes are returned.
3407 *
3408 * This function is used to determine the buffer size needed for
3409 * a weston_surface_copy_content() call.
3410 */
3411WL_EXPORT void
3412weston_surface_get_content_size(struct weston_surface *surface,
3413 int *width, int *height)
3414{
3415 struct weston_renderer *rer = surface->compositor->renderer;
3416
3417 if (!rer->surface_get_content_size) {
3418 *width = 0;
3419 *height = 0;
3420 return;
3421 }
3422
3423 rer->surface_get_content_size(surface, width, height);
3424}
3425
Quentin Glidic248dd102016-08-12 10:41:34 +02003426/** Get the bounding box of a surface and its subsurfaces
3427 *
3428 * \param surface The surface to query.
3429 * \return The bounding box relative to the surface origin.
3430 *
3431 */
3432WL_EXPORT struct weston_geometry
3433weston_surface_get_bounding_box(struct weston_surface *surface)
3434{
3435 pixman_region32_t region;
3436 pixman_box32_t *box;
3437 struct weston_subsurface *subsurface;
3438
3439 pixman_region32_init_rect(&region,
3440 0, 0,
3441 surface->width, surface->height);
3442
3443 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3444 pixman_region32_union_rect(&region, &region,
3445 subsurface->position.x,
3446 subsurface->position.y,
3447 subsurface->surface->width,
3448 subsurface->surface->height);
3449
3450 box = pixman_region32_extents(&region);
3451 struct weston_geometry geometry = {
3452 .x = box->x1,
3453 .y = box->y1,
3454 .width = box->x2 - box->x1,
3455 .height = box->y2 - box->y1,
3456 };
3457
3458 pixman_region32_fini(&region);
3459
3460 return geometry;
3461}
3462
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003463/** Copy surface contents to system memory.
3464 *
3465 * \param surface The surface to copy from.
3466 * \param target Pointer to the target memory buffer.
3467 * \param size Size of the target buffer in bytes.
3468 * \param src_x X location on contents to copy from.
3469 * \param src_y Y location on contents to copy from.
3470 * \param width Width in pixels of the area to copy.
3471 * \param height Height in pixels of the area to copy.
3472 * \return 0 for success, -1 for failure.
3473 *
3474 * Surface contents are maintained by the renderer. They can be in a
3475 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3476 * else.
3477 *
3478 * Surface contents are copied into memory pointed to by target,
3479 * which has size bytes of space available. The target memory
3480 * may be larger than needed, but being smaller returns an error.
3481 * The extra bytes in target may or may not be written; their content is
3482 * unspecified. Size must be large enough to hold the image.
3483 *
3484 * The image in the target memory will be arranged in rows from
3485 * top to bottom, and pixels on a row from left to right. The pixel
3486 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3487 * width * 4.
3488 *
3489 * Parameters src_x and src_y define the upper-left corner in buffer
3490 * coordinates (pixels) to copy from. Parameters width and height
3491 * define the size of the area to copy in pixels.
3492 *
3493 * The rectangle defined by src_x, src_y, width, height must fit in
3494 * the surface contents. Otherwise an error is returned.
3495 *
3496 * Use surface_get_data_size to determine the content size; the
3497 * needed target buffer size and rectangle limits.
3498 *
3499 * CURRENT IMPLEMENTATION RESTRICTIONS:
3500 * - the machine must be little-endian due to Pixman formats.
3501 *
3502 * NOTE: Pixman formats are premultiplied.
3503 */
3504WL_EXPORT int
3505weston_surface_copy_content(struct weston_surface *surface,
3506 void *target, size_t size,
3507 int src_x, int src_y,
3508 int width, int height)
3509{
3510 struct weston_renderer *rer = surface->compositor->renderer;
3511 int cw, ch;
3512 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3513
3514 if (!rer->surface_copy_content)
3515 return -1;
3516
3517 weston_surface_get_content_size(surface, &cw, &ch);
3518
3519 if (src_x < 0 || src_y < 0)
3520 return -1;
3521
3522 if (width <= 0 || height <= 0)
3523 return -1;
3524
3525 if (src_x + width > cw || src_y + height > ch)
3526 return -1;
3527
3528 if (width * bytespp * height > size)
3529 return -1;
3530
3531 return rer->surface_copy_content(surface, target, size,
3532 src_x, src_y, width, height);
3533}
3534
Pekka Paalanene67858b2013-04-25 13:57:42 +03003535static void
3536subsurface_set_position(struct wl_client *client,
3537 struct wl_resource *resource, int32_t x, int32_t y)
3538{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003539 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003540
3541 if (!sub)
3542 return;
3543
3544 sub->position.x = x;
3545 sub->position.y = y;
3546 sub->position.set = 1;
3547}
3548
3549static struct weston_subsurface *
3550subsurface_from_surface(struct weston_surface *surface)
3551{
3552 struct weston_subsurface *sub;
3553
3554 sub = weston_surface_to_subsurface(surface);
3555 if (sub)
3556 return sub;
3557
3558 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3559 if (sub->surface == surface)
3560 return sub;
3561
3562 return NULL;
3563}
3564
3565static struct weston_subsurface *
3566subsurface_sibling_check(struct weston_subsurface *sub,
3567 struct weston_surface *surface,
3568 const char *request)
3569{
3570 struct weston_subsurface *sibling;
3571
3572 sibling = subsurface_from_surface(surface);
3573
3574 if (!sibling) {
3575 wl_resource_post_error(sub->resource,
3576 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3577 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003578 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003579 return NULL;
3580 }
3581
3582 if (sibling->parent != sub->parent) {
3583 wl_resource_post_error(sub->resource,
3584 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3585 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003586 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003587 return NULL;
3588 }
3589
3590 return sibling;
3591}
3592
3593static void
3594subsurface_place_above(struct wl_client *client,
3595 struct wl_resource *resource,
3596 struct wl_resource *sibling_resource)
3597{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003598 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003599 struct weston_surface *surface =
3600 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003601 struct weston_subsurface *sibling;
3602
3603 if (!sub)
3604 return;
3605
3606 sibling = subsurface_sibling_check(sub, surface, "place_above");
3607 if (!sibling)
3608 return;
3609
3610 wl_list_remove(&sub->parent_link_pending);
3611 wl_list_insert(sibling->parent_link_pending.prev,
3612 &sub->parent_link_pending);
3613}
3614
3615static void
3616subsurface_place_below(struct wl_client *client,
3617 struct wl_resource *resource,
3618 struct wl_resource *sibling_resource)
3619{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003620 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003621 struct weston_surface *surface =
3622 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003623 struct weston_subsurface *sibling;
3624
3625 if (!sub)
3626 return;
3627
3628 sibling = subsurface_sibling_check(sub, surface, "place_below");
3629 if (!sibling)
3630 return;
3631
3632 wl_list_remove(&sub->parent_link_pending);
3633 wl_list_insert(&sibling->parent_link_pending,
3634 &sub->parent_link_pending);
3635}
3636
3637static void
3638subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3639{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003640 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003641
3642 if (sub)
3643 sub->synchronized = 1;
3644}
3645
3646static void
3647subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3648{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003649 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003650
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003651 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003652 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003653
3654 /* If sub became effectively desynchronized, flush. */
3655 if (!weston_subsurface_is_synchronized(sub))
3656 weston_subsurface_synchronized_commit(sub);
3657 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003658}
3659
3660static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003661weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3662{
3663 wl_list_remove(&sub->parent_link);
3664 wl_list_remove(&sub->parent_link_pending);
3665 wl_list_remove(&sub->parent_destroy_listener.link);
3666 sub->parent = NULL;
3667}
3668
3669static void
3670weston_subsurface_destroy(struct weston_subsurface *sub);
3671
3672static void
3673subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3674{
3675 struct weston_subsurface *sub =
3676 container_of(listener, struct weston_subsurface,
3677 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003678 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003679
3680 /* The protocol object (wl_resource) is left inert. */
3681 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003682 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003683
3684 weston_subsurface_destroy(sub);
3685}
3686
3687static void
3688subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3689{
3690 struct weston_subsurface *sub =
3691 container_of(listener, struct weston_subsurface,
3692 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003693 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003694 assert(sub->surface != sub->parent);
3695
3696 if (weston_surface_is_mapped(sub->surface))
3697 weston_surface_unmap(sub->surface);
3698
3699 weston_subsurface_unlink_parent(sub);
3700}
3701
3702static void
3703subsurface_resource_destroy(struct wl_resource *resource)
3704{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003705 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003706
3707 if (sub)
3708 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003709}
3710
3711static void
3712subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3713{
3714 wl_resource_destroy(resource);
3715}
3716
3717static void
3718weston_subsurface_link_parent(struct weston_subsurface *sub,
3719 struct weston_surface *parent)
3720{
3721 sub->parent = parent;
3722 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003723 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003724 &sub->parent_destroy_listener);
3725
3726 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3727 wl_list_insert(&parent->subsurface_list_pending,
3728 &sub->parent_link_pending);
3729}
3730
3731static void
3732weston_subsurface_link_surface(struct weston_subsurface *sub,
3733 struct weston_surface *surface)
3734{
3735 sub->surface = surface;
3736 sub->surface_destroy_listener.notify =
3737 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003738 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003739 &sub->surface_destroy_listener);
3740}
3741
3742static void
3743weston_subsurface_destroy(struct weston_subsurface *sub)
3744{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003745 struct weston_view *view, *next;
3746
Pekka Paalanene67858b2013-04-25 13:57:42 +03003747 assert(sub->surface);
3748
3749 if (sub->resource) {
3750 assert(weston_surface_to_subsurface(sub->surface) == sub);
3751 assert(sub->parent_destroy_listener.notify ==
3752 subsurface_handle_parent_destroy);
3753
George Kiagiadakised04d382014-06-13 18:10:26 +02003754 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3755 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003756 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003757 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003758
Pekka Paalanene67858b2013-04-25 13:57:42 +03003759 if (sub->parent)
3760 weston_subsurface_unlink_parent(sub);
3761
Jason Ekstrand7b982072014-05-20 14:33:03 -05003762 weston_surface_state_fini(&sub->cached);
3763 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003764
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003765 sub->surface->committed = NULL;
3766 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003767 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003768 } else {
3769 /* the dummy weston_subsurface for the parent itself */
3770 assert(sub->parent_destroy_listener.notify == NULL);
3771 wl_list_remove(&sub->parent_link);
3772 wl_list_remove(&sub->parent_link_pending);
3773 }
3774
3775 wl_list_remove(&sub->surface_destroy_listener.link);
3776 free(sub);
3777}
3778
3779static const struct wl_subsurface_interface subsurface_implementation = {
3780 subsurface_destroy,
3781 subsurface_set_position,
3782 subsurface_place_above,
3783 subsurface_place_below,
3784 subsurface_set_sync,
3785 subsurface_set_desync
3786};
3787
3788static struct weston_subsurface *
3789weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3790 struct weston_surface *parent)
3791{
3792 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003793 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003794
Bryce Harringtonde16d892014-11-20 22:21:57 -08003795 sub = zalloc(sizeof *sub);
3796 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003797 return NULL;
3798
Jason Ekstranda7af7042013-10-12 22:38:11 -05003799 wl_list_init(&sub->unused_views);
3800
Jason Ekstranda85118c2013-06-27 20:17:02 -05003801 sub->resource =
3802 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003803 if (!sub->resource) {
3804 free(sub);
3805 return NULL;
3806 }
3807
Jason Ekstranda85118c2013-06-27 20:17:02 -05003808 wl_resource_set_implementation(sub->resource,
3809 &subsurface_implementation,
3810 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003811 weston_subsurface_link_surface(sub, surface);
3812 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003813 weston_surface_state_init(&sub->cached);
3814 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003815 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003816
3817 return sub;
3818}
3819
3820/* Create a dummy subsurface for having the parent itself in its
3821 * sub-surface lists. Makes stacking order manipulation easy.
3822 */
3823static struct weston_subsurface *
3824weston_subsurface_create_for_parent(struct weston_surface *parent)
3825{
3826 struct weston_subsurface *sub;
3827
Bryce Harringtonde16d892014-11-20 22:21:57 -08003828 sub = zalloc(sizeof *sub);
3829 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003830 return NULL;
3831
3832 weston_subsurface_link_surface(sub, parent);
3833 sub->parent = parent;
3834 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3835 wl_list_insert(&parent->subsurface_list_pending,
3836 &sub->parent_link_pending);
3837
3838 return sub;
3839}
3840
3841static void
3842subcompositor_get_subsurface(struct wl_client *client,
3843 struct wl_resource *resource,
3844 uint32_t id,
3845 struct wl_resource *surface_resource,
3846 struct wl_resource *parent_resource)
3847{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003848 struct weston_surface *surface =
3849 wl_resource_get_user_data(surface_resource);
3850 struct weston_surface *parent =
3851 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003852 struct weston_subsurface *sub;
3853 static const char where[] = "get_subsurface: wl_subsurface@";
3854
3855 if (surface == parent) {
3856 wl_resource_post_error(resource,
3857 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3858 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003859 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003860 return;
3861 }
3862
3863 if (weston_surface_to_subsurface(surface)) {
3864 wl_resource_post_error(resource,
3865 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3866 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003867 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003868 return;
3869 }
3870
Pekka Paalanen50b67472014-10-01 15:02:41 +03003871 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3872 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003873 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003874
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003875 if (weston_surface_get_main_surface(parent) == surface) {
3876 wl_resource_post_error(resource,
3877 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3878 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003879 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003880 return;
3881 }
3882
Pekka Paalanene67858b2013-04-25 13:57:42 +03003883 /* make sure the parent is in its own list */
3884 if (wl_list_empty(&parent->subsurface_list)) {
3885 if (!weston_subsurface_create_for_parent(parent)) {
3886 wl_resource_post_no_memory(resource);
3887 return;
3888 }
3889 }
3890
3891 sub = weston_subsurface_create(id, surface, parent);
3892 if (!sub) {
3893 wl_resource_post_no_memory(resource);
3894 return;
3895 }
3896
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003897 surface->committed = subsurface_committed;
3898 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003899 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003900}
3901
3902static void
3903subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3904{
3905 wl_resource_destroy(resource);
3906}
3907
3908static const struct wl_subcompositor_interface subcompositor_interface = {
3909 subcompositor_destroy,
3910 subcompositor_get_subsurface
3911};
3912
3913static void
3914bind_subcompositor(struct wl_client *client,
3915 void *data, uint32_t version, uint32_t id)
3916{
3917 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003918 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003919
Jason Ekstranda85118c2013-06-27 20:17:02 -05003920 resource =
3921 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003922 if (resource == NULL) {
3923 wl_client_post_no_memory(client);
3924 return;
3925 }
3926 wl_resource_set_implementation(resource, &subcompositor_interface,
3927 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003928}
3929
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003930/** Set a DPMS mode on all of the compositor's outputs
3931 *
3932 * \param compositor The compositor instance
3933 * \param state The DPMS state the outputs will be set to
3934 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003935static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003936weston_compositor_dpms(struct weston_compositor *compositor,
3937 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003938{
3939 struct weston_output *output;
Bryce Harringtonf8300c82016-08-10 17:25:22 -07003940 uint32_t inhibited_outputs_mask = weston_compositor_inhibited_outputs(compositor);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003941
Bryce Harringtonf8300c82016-08-10 17:25:22 -07003942 wl_list_for_each(output, &compositor->output_list, link) {
3943 if (!output->set_dpms)
3944 continue;
3945
3946 /* If output is idle-inhibited, don't toggle to any DPMS state except ON. */
3947 if (state != WESTON_DPMS_ON &&
3948 inhibited_outputs_mask & (1 << output->id))
3949 continue;
3950
3951 output->set_dpms(output, state);
3952 }
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003953}
3954
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003955/** Restores the compositor to active status
3956 *
3957 * \param compositor The compositor instance
3958 *
3959 * If the compositor was in a sleeping mode, all outputs are powered
3960 * back on via DPMS. Otherwise if the compositor was inactive
3961 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3962 * signal will fire.
3963 *
3964 * Restarts the idle timer.
3965 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003966WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003967weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003968{
Neil Roberts8b62e202013-09-30 13:14:47 +01003969 uint32_t old_state = compositor->state;
3970
3971 /* The state needs to be changed before emitting the wake
3972 * signal because that may try to schedule a repaint which
3973 * will not work if the compositor is still sleeping */
3974 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3975
3976 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003977 case WESTON_COMPOSITOR_SLEEPING:
3978 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3979 /* fall through */
3980 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003981 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003982 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003983 /* fall through */
3984 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003985 wl_event_source_timer_update(compositor->idle_source,
3986 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003987 }
3988}
3989
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003990/** Turns off rendering and frame events for the compositor.
3991 *
3992 * \param compositor The compositor instance
3993 *
3994 * This is used for example to prevent further rendering while the
3995 * compositor is shutting down.
3996 *
3997 * \note When offscreen state is entered, outputs will be powered
3998 * back on if they were sleeping (in DPMS off mode), even though
3999 * no rendering will be performed.
4000 *
4001 * Stops the idle timer.
4002 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004003WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004004weston_compositor_offscreen(struct weston_compositor *compositor)
4005{
4006 switch (compositor->state) {
4007 case WESTON_COMPOSITOR_OFFSCREEN:
4008 return;
4009 case WESTON_COMPOSITOR_SLEEPING:
4010 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
4011 /* fall through */
4012 default:
4013 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4014 wl_event_source_timer_update(compositor->idle_source, 0);
4015 }
4016}
4017
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004018/** Powers down all attached output devices
4019 *
4020 * \param compositor The compositor instance
4021 *
4022 * Causes rendering to the outputs to cease, and no frame events to be
4023 * sent. Only powers down the outputs if the compositor is not already
4024 * in sleep mode.
4025 *
4026 * Stops the idle timer.
4027 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004028WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004029weston_compositor_sleep(struct weston_compositor *compositor)
4030{
4031 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4032 return;
4033
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004034 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004035 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4036 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4037}
4038
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004039/** Sets compositor to idle mode
4040 *
4041 * \param data The compositor instance
4042 *
4043 * This is called when the idle timer fires. Once the compositor is in
4044 * idle mode it requires a wake action (e.g. via
4045 * weston_compositor_wake()) to restore it. The compositor's
4046 * idle_signal will be triggered when the idle event occurs.
4047 *
4048 * Idleness can be inhibited by setting the compositor's idle_inhibit
4049 * property.
4050 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004051static int
4052idle_handler(void *data)
4053{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004054 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004055
4056 if (compositor->idle_inhibit)
4057 return 1;
4058
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004059 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004060 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004061
4062 return 1;
4063}
4064
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004065WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004066weston_plane_init(struct weston_plane *plane,
4067 struct weston_compositor *ec,
4068 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004069{
4070 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004071 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004072 plane->x = x;
4073 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004074 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004075
4076 /* Init the link so that the call to wl_list_remove() when releasing
4077 * the plane without ever stacking doesn't lead to a crash */
4078 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004079}
4080
4081WL_EXPORT void
4082weston_plane_release(struct weston_plane *plane)
4083{
Xiong Zhang97116532013-10-23 13:58:31 +08004084 struct weston_view *view;
4085
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004086 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004087 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004088
Xiong Zhang97116532013-10-23 13:58:31 +08004089 wl_list_for_each(view, &plane->compositor->view_list, link) {
4090 if (view->plane == plane)
4091 view->plane = NULL;
4092 }
4093
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004094 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004095}
4096
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004097WL_EXPORT void
4098weston_compositor_stack_plane(struct weston_compositor *ec,
4099 struct weston_plane *plane,
4100 struct weston_plane *above)
4101{
4102 if (above)
4103 wl_list_insert(above->link.prev, &plane->link);
4104 else
4105 wl_list_insert(&ec->plane_list, &plane->link);
4106}
4107
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004108static void
4109output_release(struct wl_client *client, struct wl_resource *resource)
4110{
4111 wl_resource_destroy(resource);
4112}
4113
4114static const struct wl_output_interface output_interface = {
4115 output_release,
4116};
4117
4118
Casey Dahlin9074db52012-04-19 22:50:09 -04004119static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004120{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004121 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004122}
4123
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004124static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004125bind_output(struct wl_client *client,
4126 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004127{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004128 struct weston_output *output = data;
4129 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004130 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004131
Jason Ekstranda85118c2013-06-27 20:17:02 -05004132 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004133 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004134 if (resource == NULL) {
4135 wl_client_post_no_memory(client);
4136 return;
4137 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004138
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004139 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004140 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004141
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004142 wl_output_send_geometry(resource,
4143 output->x,
4144 output->y,
4145 output->mm_width,
4146 output->mm_height,
4147 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004148 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004149 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004150 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004151 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004152 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004153
4154 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004155 wl_output_send_mode(resource,
4156 mode->flags,
4157 mode->width,
4158 mode->height,
4159 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004160 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004161
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004162 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004163 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004164}
4165
David Fort0de859e2016-05-27 23:22:57 +02004166/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004167static void
David Fort0de859e2016-05-27 23:22:57 +02004168weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4169 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004170{
4171 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004172 bool start_resizing = false;
4173
4174 if (!delta_width)
4175 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004176
4177 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004178 if (output == resized_output) {
4179 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004180 continue;
4181 }
4182
David Fort0de859e2016-05-27 23:22:57 +02004183 if (start_resizing) {
4184 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004185 output->dirty = 1;
4186 }
4187 }
4188}
4189
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004190WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004191weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004192{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004193 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004194 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004195
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004196 output->destroying = 1;
4197
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004198 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004199 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004200 weston_view_assign_output(view);
4201 }
4202
Pekka Paalanen0513a952014-05-21 16:17:27 +03004203 wl_event_source_remove(output->repaint_timer);
4204
Pekka Paalanen133e4392014-09-23 22:08:46 -04004205 weston_presentation_feedback_discard_list(&output->feedback_list);
4206
David Fort0de859e2016-05-27 23:22:57 +02004207 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004208 wl_list_remove(&output->link);
4209
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004210 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004211 wl_signal_emit(&output->destroy_signal, output);
4212
Richard Hughesafe690c2013-05-02 10:10:04 +01004213 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004214 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004215 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004216 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004217
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004218 wl_resource_for_each(resource, &output->resource_list) {
4219 wl_resource_set_destructor(resource, NULL);
4220 }
4221
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004222 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004223}
4224
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004225WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004226weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004227{
Scott Moreau850ca422012-05-21 15:21:25 -06004228 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004229
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004230 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004231 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004232
Scott Moreauccbf29d2012-02-22 14:21:41 -07004233 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004234 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004235 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004236 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004237 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004238 weston_matrix_scale(&output->matrix, magnification,
4239 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004240 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004241
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004242 switch (output->transform) {
4243 case WL_OUTPUT_TRANSFORM_FLIPPED:
4244 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4245 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4246 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4247 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4248 weston_matrix_scale(&output->matrix, -1, 1, 1);
4249 break;
4250 }
4251
4252 switch (output->transform) {
4253 default:
4254 case WL_OUTPUT_TRANSFORM_NORMAL:
4255 case WL_OUTPUT_TRANSFORM_FLIPPED:
4256 break;
4257 case WL_OUTPUT_TRANSFORM_90:
4258 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4259 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4260 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4261 break;
4262 case WL_OUTPUT_TRANSFORM_180:
4263 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4264 weston_matrix_translate(&output->matrix,
4265 -output->width, -output->height, 0);
4266 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4267 break;
4268 case WL_OUTPUT_TRANSFORM_270:
4269 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4270 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4271 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4272 break;
4273 }
4274
4275 if (output->current_scale != 1)
4276 weston_matrix_scale(&output->matrix,
4277 output->current_scale,
4278 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004279
Scott Moreauccbf29d2012-02-22 14:21:41 -07004280 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004281
4282 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004283}
4284
Scott Moreau1bad5db2012-08-18 01:04:05 -06004285static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004286weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004287{
4288 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004289 output->native_scale = scale;
4290 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004291
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004292 convert_size_by_transform_scale(&output->width, &output->height,
4293 output->current_mode->width,
4294 output->current_mode->height,
4295 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004296}
4297
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004298static void
4299weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004300{
4301 output->x = x;
4302 output->y = y;
4303
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004304 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004305 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004306 output->width,
4307 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004308}
4309
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004310WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004311weston_output_move(struct weston_output *output, int x, int y)
4312{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004313 struct wl_resource *resource;
4314
4315 output->move_x = x - output->x;
4316 output->move_y = y - output->y;
4317
4318 if (output->move_x == 0 && output->move_y == 0)
4319 return;
4320
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004321 weston_output_init_geometry(output, x, y);
4322
4323 output->dirty = 1;
4324
4325 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004326 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004327
4328 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004329 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004330 wl_output_send_geometry(resource,
4331 output->x,
4332 output->y,
4333 output->mm_width,
4334 output->mm_height,
4335 output->subpixel,
4336 output->make,
4337 output->model,
4338 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004339
FORT David8a120692016-04-26 23:34:06 +02004340 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004341 wl_output_send_done(resource);
4342 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004343}
4344
Bryce Harrington3f650b82015-12-23 11:01:58 -08004345/** Initialize a weston_output object's parameters
4346 *
4347 * \param output The weston_output object to initialize
4348 * \param c The output's compositor
4349 * \param x x coordinate for the output in global coordinate space
4350 * \param y y coordinate for the output in global coordinate space
4351 * \param mm_width Physical width of the output as reported by the backend
4352 * \param mm_height Physical height of the output as reported by the backend
4353 * \param transform Rotation of the output
4354 * \param scale Native scaling factor for the output
4355 *
4356 * Sets up the transformation, zoom, and geometry of the output using
4357 * the input properties.
4358 *
4359 * Establishes a repaint timer for the output with the relevant display
4360 * object's event loop. See output_repaint_timer_handler().
4361 *
4362 * The output is assigned an ID. Weston can support up to 32 distinct
4363 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4364 * is referred to and used to find the first available ID number, and
4365 * then this ID is marked as used in output_id_pool.
4366 *
4367 * The output is also assigned a Wayland global with the wl_output
4368 * external interface.
4369 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004370WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004371weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004372 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004373 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004374{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004375 struct wl_event_loop *loop;
4376
Bryce Harrington18e45732015-12-23 20:53:53 -08004377 /* Verify we haven't reached the limit of 32 available output IDs */
4378 assert(ffs(~c->output_id_pool) > 0);
4379
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004380 output->compositor = c;
4381 output->x = x;
4382 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004383 output->mm_width = mm_width;
4384 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004385 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004386 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004387
Alexander Larsson0b135062013-05-28 16:23:36 +02004388 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004389 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004390
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004391 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004392 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004393
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004394 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004395 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004396 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004397 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004398 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004399 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004400
Pekka Paalanen0513a952014-05-21 16:17:27 +03004401 loop = wl_display_get_event_loop(c->wl_display);
4402 output->repaint_timer = wl_event_loop_add_timer(loop,
4403 output_repaint_timer_handler, output);
4404
Bryce Harrington3f650b82015-12-23 11:01:58 -08004405 /* Invert the output id pool and look for the lowest numbered
4406 * switch (the least significant bit). Take that bit's position
4407 * as our ID, and mark it used in the compositor's output_id_pool.
4408 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004409 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004410 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004411
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004412 output->global =
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004413 wl_global_create(c->wl_display, &wl_output_interface, 3,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004414 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004415}
4416
4417/** Adds an output to the compositor's output list and
4418 * send the compositor's output_created signal.
4419 *
4420 * \param compositor The compositor instance.
4421 * \param output The output to be added.
4422 */
4423WL_EXPORT void
4424weston_compositor_add_output(struct weston_compositor *compositor,
4425 struct weston_output *output)
4426{
Armin Krezoviće5403842016-08-05 15:28:29 +02004427 struct weston_view *view, *next;
4428
Giulio Camuffob1147152015-05-06 21:41:57 +03004429 wl_list_insert(compositor->output_list.prev, &output->link);
4430 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02004431
4432 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4433 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004434}
4435
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004436WL_EXPORT void
4437weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004438 double device_x, double device_y,
4439 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004440{
Derek Foreman0f679412014-10-02 13:41:17 -05004441 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004442 device_x,
4443 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004444 0.0,
4445 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004446
Derek Foreman67a18b92015-03-24 11:36:14 -05004447 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004448
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004449 *x = p.f[0] / p.f[3];
4450 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004451}
4452
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004453static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004454destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004455{
Jonny Lamb74130762013-11-26 18:19:46 +01004456 struct weston_surface *surface =
4457 wl_resource_get_user_data(resource);
4458
Pekka Paalanen4826f872016-04-22 14:14:38 +03004459 if (!surface)
4460 return;
4461
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004462 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004463 surface->pending.buffer_viewport.buffer.src_width =
4464 wl_fixed_from_int(-1);
4465 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004466 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004467}
4468
4469static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004470viewport_destroy(struct wl_client *client,
4471 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004472{
4473 wl_resource_destroy(resource);
4474}
4475
4476static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004477viewport_set_source(struct wl_client *client,
4478 struct wl_resource *resource,
4479 wl_fixed_t src_x,
4480 wl_fixed_t src_y,
4481 wl_fixed_t src_width,
4482 wl_fixed_t src_height)
4483{
4484 struct weston_surface *surface =
4485 wl_resource_get_user_data(resource);
4486
Pekka Paalanen4826f872016-04-22 14:14:38 +03004487 if (!surface) {
4488 wl_resource_post_error(resource,
4489 WP_VIEWPORT_ERROR_NO_SURFACE,
4490 "wl_surface for this viewport is no longer exists");
4491 return;
4492 }
4493
4494 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004495 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004496
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004497 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004498 src_height == wl_fixed_from_int(-1) &&
4499 src_x == wl_fixed_from_int(-1) &&
4500 src_y == wl_fixed_from_int(-1)) {
4501 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004502 surface->pending.buffer_viewport.buffer.src_width =
4503 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004504 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004505 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004506 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004507
Pekka Paalanen201769a2016-04-26 14:42:11 +03004508 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004509 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004510 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004511 "wl_surface@%d viewport source "
4512 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4513 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004514 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004515 wl_fixed_to_double(src_height),
4516 wl_fixed_to_double(src_x),
4517 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004518 return;
4519 }
4520
4521 surface->pending.buffer_viewport.buffer.src_x = src_x;
4522 surface->pending.buffer_viewport.buffer.src_y = src_y;
4523 surface->pending.buffer_viewport.buffer.src_width = src_width;
4524 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004525 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004526}
4527
4528static void
4529viewport_set_destination(struct wl_client *client,
4530 struct wl_resource *resource,
4531 int32_t dst_width,
4532 int32_t dst_height)
4533{
4534 struct weston_surface *surface =
4535 wl_resource_get_user_data(resource);
4536
Pekka Paalanen4826f872016-04-22 14:14:38 +03004537 if (!surface) {
4538 wl_resource_post_error(resource,
4539 WP_VIEWPORT_ERROR_NO_SURFACE,
4540 "wl_surface for this viewport no longer exists");
4541 return;
4542 }
4543
4544 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004545
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004546 if (dst_width == -1 && dst_height == -1) {
4547 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004548 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004549 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004550 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004551 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004552
4553 if (dst_width <= 0 || dst_height <= 0) {
4554 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004555 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004556 "destination size must be positive (%dx%d)",
4557 dst_width, dst_height);
4558 return;
4559 }
4560
4561 surface->pending.buffer_viewport.surface.width = dst_width;
4562 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004563 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004564}
4565
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004566static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004567 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004568 viewport_set_source,
4569 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004570};
4571
4572static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004573viewporter_destroy(struct wl_client *client,
4574 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004575{
4576 wl_resource_destroy(resource);
4577}
4578
4579static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004580viewporter_get_viewport(struct wl_client *client,
4581 struct wl_resource *viewporter,
4582 uint32_t id,
4583 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004584{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004585 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004586 struct weston_surface *surface =
4587 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004588 struct wl_resource *resource;
4589
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004590 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004591 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004592 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004593 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004594 return;
4595 }
4596
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004597 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004598 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004599 if (resource == NULL) {
4600 wl_client_post_no_memory(client);
4601 return;
4602 }
4603
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004604 wl_resource_set_implementation(resource, &viewport_interface,
4605 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004606
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004607 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004608}
4609
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004610static const struct wp_viewporter_interface viewporter_interface = {
4611 viewporter_destroy,
4612 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004613};
4614
4615static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004616bind_viewporter(struct wl_client *client,
4617 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004618{
4619 struct wl_resource *resource;
4620
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004621 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004622 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004623 if (resource == NULL) {
4624 wl_client_post_no_memory(client);
4625 return;
4626 }
4627
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004628 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004629 NULL, NULL);
4630}
4631
4632static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004633destroy_presentation_feedback(struct wl_resource *feedback_resource)
4634{
4635 struct weston_presentation_feedback *feedback;
4636
4637 feedback = wl_resource_get_user_data(feedback_resource);
4638
4639 wl_list_remove(&feedback->link);
4640 free(feedback);
4641}
4642
4643static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004644presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4645{
4646 wl_resource_destroy(resource);
4647}
4648
4649static void
4650presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004651 struct wl_resource *presentation_resource,
4652 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004653 uint32_t callback)
4654{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004655 struct weston_surface *surface;
4656 struct weston_presentation_feedback *feedback;
4657
4658 surface = wl_resource_get_user_data(surface_resource);
4659
Bryce Harringtonde16d892014-11-20 22:21:57 -08004660 feedback = zalloc(sizeof *feedback);
4661 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004662 goto err_calloc;
4663
4664 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004665 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004666 1, callback);
4667 if (!feedback->resource)
4668 goto err_create;
4669
4670 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4671 destroy_presentation_feedback);
4672
4673 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4674
4675 return;
4676
4677err_create:
4678 free(feedback);
4679
4680err_calloc:
4681 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004682}
4683
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004684static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004685 presentation_destroy,
4686 presentation_feedback
4687};
4688
4689static void
4690bind_presentation(struct wl_client *client,
4691 void *data, uint32_t version, uint32_t id)
4692{
4693 struct weston_compositor *compositor = data;
4694 struct wl_resource *resource;
4695
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004696 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004697 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004698 if (resource == NULL) {
4699 wl_client_post_no_memory(client);
4700 return;
4701 }
4702
4703 wl_resource_set_implementation(resource, &presentation_implementation,
4704 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004705 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004706}
4707
4708static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004709compositor_bind(struct wl_client *client,
4710 void *data, uint32_t version, uint32_t id)
4711{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004712 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004713 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004714
Jason Ekstranda85118c2013-06-27 20:17:02 -05004715 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004716 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004717 if (resource == NULL) {
4718 wl_client_post_no_memory(client);
4719 return;
4720 }
4721
4722 wl_resource_set_implementation(resource, &compositor_interface,
4723 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004724}
4725
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004726WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004727weston_environment_get_fd(const char *env)
4728{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07004729 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004730 int fd, flags;
4731
4732 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07004733 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004734 return -1;
4735
4736 flags = fcntl(fd, F_GETFD);
4737 if (flags == -1)
4738 return -1;
4739
4740 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4741 unsetenv(env);
4742
4743 return fd;
4744}
4745
Pekka Paalanenb5026542014-11-12 15:09:24 +02004746static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004747timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004748 uint32_t key, void *data)
4749{
4750 struct weston_compositor *compositor = data;
4751
4752 if (weston_timeline_enabled_)
4753 weston_timeline_close();
4754 else
4755 weston_timeline_open(compositor);
4756}
4757
Giulio Camuffo459137b2014-10-11 23:56:24 +03004758/** Create the compositor.
4759 *
4760 * This functions creates and initializes a compositor instance.
4761 *
4762 * \param display The Wayland display to be used.
4763 * \param user_data A pointer to an object that can later be retrieved
4764 * using the \ref weston_compositor_get_user_data function.
4765 * \return The compositor instance on success or NULL on failure.
4766 */
4767WL_EXPORT struct weston_compositor *
4768weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004769{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004770 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004771 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004772
Giulio Camuffo459137b2014-10-11 23:56:24 +03004773 ec = zalloc(sizeof *ec);
4774 if (!ec)
4775 return NULL;
4776
4777 ec->wl_display = display;
4778 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004779 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004780 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004781 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004782 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004783 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004784 wl_signal_init(&ec->idle_signal);
4785 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004786 wl_signal_init(&ec->show_input_panel_signal);
4787 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004788 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004789 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004790 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004791 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004792 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004793 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004794 wl_signal_init(&ec->session_signal);
4795 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004796
Casey Dahlin58ba1372012-04-19 22:50:08 -04004797 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004798 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004799
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02004800 ec->activate_serial = 1;
4801
Derek Foreman152254b2015-11-26 14:17:48 -06004802 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004803 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004804 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004805
Giulio Camuffo954f1832014-10-11 18:27:30 +03004806 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004807 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004808 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004809
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004810 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004811 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004812 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004813
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004814 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004815 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004816 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004817
Jonas Ådahl30d61d82014-10-22 21:21:17 +02004818 if (weston_input_init(ec) != 0)
4819 goto fail;
4820
Jason Ekstranda7af7042013-10-12 22:38:11 -05004821 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004822 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004823 wl_list_init(&ec->layer_list);
4824 wl_list_init(&ec->seat_list);
4825 wl_list_init(&ec->output_list);
4826 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004827 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004828 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004829 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004830 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004831 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004832
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004833 wl_list_init(&ec->plugin_api_list);
4834
Xiong Zhang97116532013-10-23 13:58:31 +08004835 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004836 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004837
Giulio Camuffo459137b2014-10-11 23:56:24 +03004838 wl_data_device_manager_init(ec->wl_display);
4839
4840 wl_display_init_shm(ec->wl_display);
4841
4842 loop = wl_display_get_event_loop(ec->wl_display);
4843 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004844
Giulio Camuffo459137b2014-10-11 23:56:24 +03004845 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4846 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4847
4848 weston_compositor_add_debug_binding(ec, KEY_T,
4849 timeline_key_binding_handler, ec);
4850
Giulio Camuffo459137b2014-10-11 23:56:24 +03004851 return ec;
4852
4853fail:
4854 free(ec);
4855 return NULL;
4856}
4857
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004858WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004859weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004860{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004861 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004862
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004863 wl_event_source_remove(ec->idle_source);
4864
Matt Roper361d2ad2011-08-29 13:52:23 -07004865 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004866 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004867 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004868
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004869 if (ec->renderer)
4870 ec->renderer->destroy(ec);
4871
Daniel Stone325fc2d2012-05-30 16:31:58 +01004872 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004873 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004874 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004875 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004876 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004877 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004878
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004879 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004880}
4881
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004882WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004883weston_compositor_exit_with_code(struct weston_compositor *compositor,
4884 int exit_code)
4885{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004886 if (compositor->exit_code == EXIT_SUCCESS)
4887 compositor->exit_code = exit_code;
4888
Giulio Camuffo459137b2014-10-11 23:56:24 +03004889 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004890}
4891
4892WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004893weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4894 const struct weston_pointer_grab_interface *interface)
4895{
4896 struct weston_seat *seat;
4897
4898 ec->default_pointer_grab = interface;
4899 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004900 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4901
4902 if (pointer)
4903 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004904 }
4905}
4906
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004907WL_EXPORT int
4908weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4909 clockid_t clk_id)
4910{
4911 struct timespec ts;
4912
4913 if (clock_gettime(clk_id, &ts) < 0)
4914 return -1;
4915
4916 compositor->presentation_clock = clk_id;
4917
4918 return 0;
4919}
4920
4921/*
4922 * For choosing the software clock, when the display hardware or API
4923 * does not expose a compatible presentation timestamp.
4924 */
4925WL_EXPORT int
4926weston_compositor_set_presentation_clock_software(
4927 struct weston_compositor *compositor)
4928{
4929 /* In order of preference */
4930 static const clockid_t clocks[] = {
4931 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4932 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4933 CLOCK_MONOTONIC, /* no jumps, may crawl */
4934 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4935 CLOCK_REALTIME /* may jump and crawl */
4936 };
4937 unsigned i;
4938
4939 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4940 if (weston_compositor_set_presentation_clock(compositor,
4941 clocks[i]) == 0)
4942 return 0;
4943
4944 weston_log("Error: no suitable presentation clock available.\n");
4945
4946 return -1;
4947}
4948
Pekka Paalanen662f3842015-03-18 12:17:26 +02004949/** Read the current time from the Presentation clock
4950 *
4951 * \param compositor
4952 * \param ts[out] The current time.
4953 *
4954 * \note Reading the current time in user space is always imprecise to some
4955 * degree.
4956 *
4957 * This function is never meant to fail. If reading the clock does fail,
4958 * an error message is logged and a zero time is returned. Callers are not
4959 * supposed to detect or react to failures.
4960 */
4961WL_EXPORT void
4962weston_compositor_read_presentation_clock(
4963 const struct weston_compositor *compositor,
4964 struct timespec *ts)
4965{
4966 static bool warned;
4967 int ret;
4968
4969 ret = clock_gettime(compositor->presentation_clock, ts);
4970 if (ret < 0) {
4971 ts->tv_sec = 0;
4972 ts->tv_nsec = 0;
4973
4974 if (!warned)
4975 weston_log("Error: failure to read "
4976 "the presentation clock %#x: '%m' (%d)\n",
4977 compositor->presentation_clock, errno);
4978 warned = true;
4979 }
4980}
4981
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004982/** Import dmabuf buffer into current renderer
4983 *
4984 * \param compositor
4985 * \param buffer the dmabuf buffer to import
4986 * \return true on usable buffers, false otherwise
4987 *
4988 * This function tests that the linux_dmabuf_buffer is usable
4989 * for the current renderer. Returns false on unusable buffers. Usually
4990 * usability is tested by importing the dmabufs for composition.
4991 *
4992 * This hook is also used for detecting if the renderer supports
4993 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4994 * supported.
4995 * */
4996WL_EXPORT bool
4997weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4998 struct linux_dmabuf_buffer *buffer)
4999{
5000 struct weston_renderer *renderer;
5001
5002 renderer = compositor->renderer;
5003
5004 if (renderer->import_dmabuf == NULL)
5005 return false;
5006
5007 return renderer->import_dmabuf(compositor, buffer);
5008}
5009
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005010WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005011weston_version(int *major, int *minor, int *micro)
5012{
5013 *major = WESTON_VERSION_MAJOR;
5014 *minor = WESTON_VERSION_MINOR;
5015 *micro = WESTON_VERSION_MICRO;
5016}
5017
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005018WL_EXPORT void *
5019weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005020{
Derek Foreman3f86e502015-06-08 11:46:54 -05005021 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005022 char path[PATH_MAX];
5023 void *module, *init;
5024
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005025 if (name == NULL)
5026 return NULL;
5027
Derek Foreman3f86e502015-06-08 11:46:54 -05005028 if (name[0] != '/') {
5029 if (builddir)
5030 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
5031 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03005032 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005033 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02005034 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005035 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005036
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005037 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
5038 if (module) {
5039 weston_log("Module '%s' already loaded\n", path);
5040 dlclose(module);
5041 return NULL;
5042 }
5043
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005044 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04005045 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005046 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005047 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005048 return NULL;
5049 }
5050
5051 init = dlsym(module, entrypoint);
5052 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005053 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00005054 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005055 return NULL;
5056 }
5057
5058 return init;
5059}
5060
Giulio Camuffo459137b2014-10-11 23:56:24 +03005061
5062/** Destroys the compositor.
5063 *
5064 * This function cleans up the compositor state and destroys it.
5065 *
5066 * \param compositor The compositor to be destroyed.
5067 */
5068WL_EXPORT void
5069weston_compositor_destroy(struct weston_compositor *compositor)
5070{
5071 /* prevent further rendering while shutting down */
5072 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5073
5074 wl_signal_emit(&compositor->destroy_signal, compositor);
5075
5076 weston_compositor_xkb_destroy(compositor);
5077
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005078 if (compositor->backend)
5079 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005080
5081 weston_plugin_api_destroy_list(compositor);
5082
Giulio Camuffo459137b2014-10-11 23:56:24 +03005083 free(compositor);
5084}
5085
5086/** Instruct the compositor to exit.
5087 *
5088 * This functions does not directly destroy the compositor object, it merely
5089 * command it to start the tear down process. It is not guaranteed that the
5090 * tear down will happen immediately.
5091 *
5092 * \param compositor The compositor to tear down.
5093 */
5094WL_EXPORT void
5095weston_compositor_exit(struct weston_compositor *compositor)
5096{
5097 compositor->exit(compositor);
5098}
5099
5100/** Return the user data stored in the compositor.
5101 *
5102 * This function returns the user data pointer set with user_data parameter
5103 * to the \ref weston_compositor_create function.
5104 */
5105WL_EXPORT void *
5106weston_compositor_get_user_data(struct weston_compositor *compositor)
5107{
5108 return compositor->user_data;
5109}
Pekka Paalanendd186732016-06-03 14:49:54 +03005110
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005111static const char * const backend_map[] = {
5112 [WESTON_BACKEND_DRM] = "drm-backend.so",
5113 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5114 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5115 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5116 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5117 [WESTON_BACKEND_X11] = "x11-backend.so",
5118};
5119
Pekka Paalanendd186732016-06-03 14:49:54 +03005120/** Load a backend into a weston_compositor
5121 *
5122 * A backend must be loaded to make a weston_compositor work. A backend
5123 * provides input and output capabilities, and determines the renderer to use.
5124 *
5125 * \param compositor A compositor that has not had a backend loaded yet.
5126 * \param backend Name of the backend file.
5127 * \param config_base A pointer to a backend-specific configuration
5128 * structure's 'base' member.
5129 *
5130 * \return 0 on success, or -1 on error.
5131 */
5132WL_EXPORT int
5133weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005134 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005135 struct weston_backend_config *config_base)
5136{
5137 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005138 struct weston_backend_config *config_base);
5139
Quentin Glidic887c0182016-07-10 11:00:53 +02005140 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005141 return -1;
5142
5143 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005144 if (!backend_init)
5145 return -1;
5146
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005147 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005148}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005149
5150WL_EXPORT int
5151weston_compositor_load_xwayland(struct weston_compositor *compositor)
5152{
5153 int (*module_init)(struct weston_compositor *ec,
5154 int *argc, char *argv[]);
5155 int argc = 0;
5156
5157 module_init = weston_load_module("xwayland.so", "module_init");
5158 if (!module_init)
5159 return -1;
5160 if (module_init(compositor, &argc, NULL) < 0)
5161 return -1;
5162 return 0;
5163}