blob: 4a0bfef2ac33765e6950b0c4dd2d7c3cab8edc1a [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"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030061#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040062#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050063#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020064#include "plugin-registry.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050065
Pekka Paalanen0513a952014-05-21 16:17:27 +030066#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
67
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020068static void
Alexander Larsson0b135062013-05-28 16:23:36 +020069weston_output_transform_scale_init(struct weston_output *output,
70 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020071
Rob Bradford27b17932013-06-26 18:08:46 +010072static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050073weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010074
Derek Foreman6ae7bc92014-11-04 10:47:33 -060075static void weston_mode_switch_finish(struct weston_output *output,
76 int mode_changed,
77 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080078{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020079 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020080 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020081 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060082 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020083
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020084 pixman_region32_init(&old_output_region);
85 pixman_region32_copy(&old_output_region, &output->region);
86
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020087 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020088 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020089
90 pixman_region32_init(&output->previous_damage);
91 pixman_region32_init_rect(&output->region, output->x, output->y,
92 output->width, output->height);
93
94 weston_output_update_matrix(output);
95
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020096 /* If a pointer falls outside the outputs new geometry, move it to its
97 * lower-right corner */
98 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -050099 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200100 int32_t x, y;
101
102 if (!pointer)
103 continue;
104
105 x = wl_fixed_to_int(pointer->x);
106 y = wl_fixed_to_int(pointer->y);
107
108 if (!pixman_region32_contains_point(&old_output_region,
109 x, y, NULL) ||
110 pixman_region32_contains_point(&output->region,
111 x, y, NULL))
112 continue;
113
114 if (x >= output->x + output->width)
115 x = output->x + output->width - 1;
116 if (y >= output->y + output->height)
117 y = output->y + output->height - 1;
118
119 pointer->x = wl_fixed_from_int(x);
120 pointer->y = wl_fixed_from_int(y);
121 }
122
123 pixman_region32_fini(&old_output_region);
124
Derek Foremandd4cd332014-11-10 10:29:59 -0600125 if (!mode_changed && !scale_changed)
126 return;
127
Hardening57388e42013-09-18 23:56:36 +0200128 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600129 wl_resource_for_each(resource, &output->resource_list) {
130 if (mode_changed) {
131 wl_output_send_mode(resource,
132 output->current_mode->flags,
133 output->current_mode->width,
134 output->current_mode->height,
135 output->current_mode->refresh);
136 }
Hardening57388e42013-09-18 23:56:36 +0200137
Derek Foreman41bdc272014-11-05 13:26:57 -0600138 version = wl_resource_get_version(resource);
139 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600140 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200141
Derek Foreman41bdc272014-11-05 13:26:57 -0600142 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
143 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600144 }
145}
146
David Fort0de859e2016-05-27 23:22:57 +0200147
148static void
149weston_compositor_reflow_outputs(struct weston_compositor *compositor,
150 struct weston_output *resized_output, int delta_width);
151
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600152WL_EXPORT int
153weston_output_mode_set_native(struct weston_output *output,
154 struct weston_mode *mode,
155 int32_t scale)
156{
157 int ret;
158 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200159 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600160
161 if (!output->switch_mode)
162 return -1;
163
164 if (!output->original_mode) {
165 mode_changed = 1;
166 ret = output->switch_mode(output, mode);
167 if (ret < 0)
168 return ret;
169 if (output->current_scale != scale) {
170 scale_changed = 1;
171 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200172 }
173 }
174
David Fort0de859e2016-05-27 23:22:57 +0200175 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600176 output->native_mode = mode;
177 output->native_scale = scale;
178
179 weston_mode_switch_finish(output, mode_changed, scale_changed);
180
David Fort0de859e2016-05-27 23:22:57 +0200181 if (mode_changed || scale_changed) {
182 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
183
184 wl_signal_emit(&output->compositor->output_resized_signal, output);
185 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600186 return 0;
187}
188
189WL_EXPORT int
190weston_output_mode_switch_to_native(struct weston_output *output)
191{
192 int ret;
193 int mode_changed = 0, scale_changed = 0;
194
195 if (!output->switch_mode)
196 return -1;
197
198 if (!output->original_mode) {
199 weston_log("already in the native mode\n");
200 return -1;
201 }
202 /* the non fullscreen clients haven't seen a mode set since we
203 * switched into a temporary, so we need to notify them if the
204 * mode at that time is different from the native mode now.
205 */
206 mode_changed = (output->original_mode != output->native_mode);
207 scale_changed = (output->original_scale != output->native_scale);
208
209 ret = output->switch_mode(output, output->native_mode);
210 if (ret < 0)
211 return ret;
212
213 output->current_scale = output->native_scale;
214
215 output->original_mode = NULL;
216 output->original_scale = 0;
217
218 weston_mode_switch_finish(output, mode_changed, scale_changed);
219
220 return 0;
221}
222
223WL_EXPORT int
224weston_output_mode_switch_to_temporary(struct weston_output *output,
225 struct weston_mode *mode,
226 int32_t scale)
227{
228 int ret;
229
230 if (!output->switch_mode)
231 return -1;
232
233 /* original_mode is the last mode non full screen clients have seen,
234 * so we shouldn't change it if we already have one set.
235 */
236 if (!output->original_mode) {
237 output->original_mode = output->native_mode;
238 output->original_scale = output->native_scale;
239 }
240 ret = output->switch_mode(output, mode);
241 if (ret < 0)
242 return ret;
243
244 output->current_scale = scale;
245
246 weston_mode_switch_finish(output, 0, 0);
247
248 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800249}
250
Benjamin Franzke06286262011-05-06 19:12:33 +0200251static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300252region_init_infinite(pixman_region32_t *region)
253{
254 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
255 UINT32_MAX, UINT32_MAX);
256}
257
Pekka Paalanene67858b2013-04-25 13:57:42 +0300258static struct weston_subsurface *
259weston_surface_to_subsurface(struct weston_surface *surface);
260
Jason Ekstranda7af7042013-10-12 22:38:11 -0500261WL_EXPORT struct weston_view *
262weston_view_create(struct weston_surface *surface)
263{
264 struct weston_view *view;
265
Bryce Harringtonde16d892014-11-20 22:21:57 -0800266 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500267 if (view == NULL)
268 return NULL;
269
270 view->surface = surface;
271
Jason Ekstranda7af7042013-10-12 22:38:11 -0500272 /* Assign to surface */
273 wl_list_insert(&surface->views, &view->surface_link);
274
275 wl_signal_init(&view->destroy_signal);
276 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300277 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278
Jason Ekstranda7af7042013-10-12 22:38:11 -0500279 pixman_region32_init(&view->clip);
280
281 view->alpha = 1.0;
282 pixman_region32_init(&view->transform.opaque);
283
284 wl_list_init(&view->geometry.transformation_list);
285 wl_list_insert(&view->geometry.transformation_list,
286 &view->transform.position.link);
287 weston_matrix_init(&view->transform.position.matrix);
288 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200289 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500290 pixman_region32_init(&view->transform.boundingbox);
291 view->transform.dirty = 1;
292
Jason Ekstranda7af7042013-10-12 22:38:11 -0500293 return view;
294}
295
Jason Ekstrand108865d2014-06-26 10:04:49 -0700296struct weston_frame_callback {
297 struct wl_resource *resource;
298 struct wl_list link;
299};
300
Pekka Paalanen133e4392014-09-23 22:08:46 -0400301struct weston_presentation_feedback {
302 struct wl_resource *resource;
303
304 /* XXX: could use just wl_resource_get_link() instead */
305 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200306
307 /* The per-surface feedback flags */
308 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400309};
310
311static void
312weston_presentation_feedback_discard(
313 struct weston_presentation_feedback *feedback)
314{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200315 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400316 wl_resource_destroy(feedback->resource);
317}
318
319static void
320weston_presentation_feedback_discard_list(struct wl_list *list)
321{
322 struct weston_presentation_feedback *feedback, *tmp;
323
324 wl_list_for_each_safe(feedback, tmp, list, link)
325 weston_presentation_feedback_discard(feedback);
326}
327
328static void
329weston_presentation_feedback_present(
330 struct weston_presentation_feedback *feedback,
331 struct weston_output *output,
332 uint32_t refresh_nsec,
333 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200334 uint64_t seq,
335 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400336{
337 struct wl_client *client = wl_resource_get_client(feedback->resource);
338 struct wl_resource *o;
339 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400340
341 wl_resource_for_each(o, &output->resource_list) {
342 if (wl_resource_get_client(o) != client)
343 continue;
344
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200345 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400346 }
347
348 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200349 wp_presentation_feedback_send_presented(feedback->resource,
350 secs >> 32, secs & 0xffffffff,
351 ts->tv_nsec,
352 refresh_nsec,
353 seq >> 32, seq & 0xffffffff,
354 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400355 wl_resource_destroy(feedback->resource);
356}
357
358static void
359weston_presentation_feedback_present_list(struct wl_list *list,
360 struct weston_output *output,
361 uint32_t refresh_nsec,
362 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200363 uint64_t seq,
364 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400365{
366 struct weston_presentation_feedback *feedback, *tmp;
367
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200368 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200369 wl_list_empty(list));
370
Pekka Paalanen133e4392014-09-23 22:08:46 -0400371 wl_list_for_each_safe(feedback, tmp, list, link)
372 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200373 refresh_nsec, ts, seq,
374 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400375}
376
Jason Ekstrand7b982072014-05-20 14:33:03 -0500377static void
378surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
379{
380 struct weston_surface_state *state =
381 container_of(listener, struct weston_surface_state,
382 buffer_destroy_listener);
383
384 state->buffer = NULL;
385}
386
387static void
388weston_surface_state_init(struct weston_surface_state *state)
389{
390 state->newly_attached = 0;
391 state->buffer = NULL;
392 state->buffer_destroy_listener.notify =
393 surface_state_handle_buffer_destroy;
394 state->sx = 0;
395 state->sy = 0;
396
Derek Foreman152254b2015-11-26 14:17:48 -0600397 pixman_region32_init(&state->damage_surface);
398 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500399 pixman_region32_init(&state->opaque);
400 region_init_infinite(&state->input);
401
402 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400403 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500404
405 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
406 state->buffer_viewport.buffer.scale = 1;
407 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
408 state->buffer_viewport.surface.width = -1;
409 state->buffer_viewport.changed = 0;
410}
411
412static void
413weston_surface_state_fini(struct weston_surface_state *state)
414{
415 struct weston_frame_callback *cb, *next;
416
417 wl_list_for_each_safe(cb, next,
418 &state->frame_callback_list, link)
419 wl_resource_destroy(cb->resource);
420
Pekka Paalanen133e4392014-09-23 22:08:46 -0400421 weston_presentation_feedback_discard_list(&state->feedback_list);
422
Jason Ekstrand7b982072014-05-20 14:33:03 -0500423 pixman_region32_fini(&state->input);
424 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600425 pixman_region32_fini(&state->damage_surface);
426 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500427
428 if (state->buffer)
429 wl_list_remove(&state->buffer_destroy_listener.link);
430 state->buffer = NULL;
431}
432
433static void
434weston_surface_state_set_buffer(struct weston_surface_state *state,
435 struct weston_buffer *buffer)
436{
437 if (state->buffer == buffer)
438 return;
439
440 if (state->buffer)
441 wl_list_remove(&state->buffer_destroy_listener.link);
442 state->buffer = buffer;
443 if (state->buffer)
444 wl_signal_add(&state->buffer->destroy_signal,
445 &state->buffer_destroy_listener);
446}
447
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500448WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500449weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500450{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500451 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400452
Bryce Harringtonde16d892014-11-20 22:21:57 -0800453 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400454 if (surface == NULL)
455 return NULL;
456
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500457 wl_signal_init(&surface->destroy_signal);
458
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500459 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200460 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400461
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200462 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
463 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200464 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
465 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500466
467 weston_surface_state_init(&surface->pending);
468
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400469 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500470 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300471 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400472
Jason Ekstranda7af7042013-10-12 22:38:11 -0500473 wl_list_init(&surface->views);
474
475 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400476 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500477
Pekka Paalanene67858b2013-04-25 13:57:42 +0300478 wl_list_init(&surface->subsurface_list);
479 wl_list_init(&surface->subsurface_list_pending);
480
Jason Ekstrand1e059042014-10-16 10:55:19 -0500481 weston_matrix_init(&surface->buffer_to_surface_matrix);
482 weston_matrix_init(&surface->surface_to_buffer_matrix);
483
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400484 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500485}
486
Alex Wu8811bf92012-02-28 18:07:54 +0800487WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500488weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200489 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500490{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100491 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500492}
493
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400494WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500495weston_view_to_global_float(struct weston_view *view,
496 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200497{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500498 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200499 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
500
Jason Ekstranda7af7042013-10-12 22:38:11 -0500501 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200502
503 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200504 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700505 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200506 v.f[3]);
507 *x = 0;
508 *y = 0;
509 return;
510 }
511
512 *x = v.f[0] / v.f[3];
513 *y = v.f[1] / v.f[3];
514 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500515 *x = sx + view->geometry.x;
516 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200517 }
518}
519
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500520WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200521weston_transformed_coord(int width, int height,
522 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200523 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200524 float sx, float sy, float *bx, float *by)
525{
526 switch (transform) {
527 case WL_OUTPUT_TRANSFORM_NORMAL:
528 default:
529 *bx = sx;
530 *by = sy;
531 break;
532 case WL_OUTPUT_TRANSFORM_FLIPPED:
533 *bx = width - sx;
534 *by = sy;
535 break;
536 case WL_OUTPUT_TRANSFORM_90:
537 *bx = height - sy;
538 *by = sx;
539 break;
540 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
541 *bx = height - sy;
542 *by = width - sx;
543 break;
544 case WL_OUTPUT_TRANSFORM_180:
545 *bx = width - sx;
546 *by = height - sy;
547 break;
548 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
549 *bx = sx;
550 *by = height - sy;
551 break;
552 case WL_OUTPUT_TRANSFORM_270:
553 *bx = sy;
554 *by = width - sx;
555 break;
556 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
557 *bx = sy;
558 *by = sx;
559 break;
560 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200561
562 *bx *= scale;
563 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200564}
565
566WL_EXPORT pixman_box32_t
567weston_transformed_rect(int width, int height,
568 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200569 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200570 pixman_box32_t rect)
571{
572 float x1, x2, y1, y2;
573
574 pixman_box32_t ret;
575
Alexander Larsson4ea95522013-05-22 14:41:37 +0200576 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200577 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200578 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200579 rect.x2, rect.y2, &x2, &y2);
580
581 if (x1 <= x2) {
582 ret.x1 = x1;
583 ret.x2 = x2;
584 } else {
585 ret.x1 = x2;
586 ret.x2 = x1;
587 }
588
589 if (y1 <= y2) {
590 ret.y1 = y1;
591 ret.y2 = y2;
592 } else {
593 ret.y1 = y2;
594 ret.y2 = y1;
595 }
596
597 return ret;
598}
599
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600600/** Transform a region by a matrix, restricted to axis-aligned transformations
601 *
602 * Warning: This function does not work for projective, affine, or matrices
603 * that encode arbitrary rotations. Only 90-degree step rotations are
604 * supported.
605 */
606WL_EXPORT void
607weston_matrix_transform_region(pixman_region32_t *dest,
608 struct weston_matrix *matrix,
609 pixman_region32_t *src)
610{
611 pixman_box32_t *src_rects, *dest_rects;
612 int nrects, i;
613
614 src_rects = pixman_region32_rectangles(src, &nrects);
615 dest_rects = malloc(nrects * sizeof(*dest_rects));
616 if (!dest_rects)
617 return;
618
619 for (i = 0; i < nrects; i++) {
620 struct weston_vector vec1 = {{
621 src_rects[i].x1, src_rects[i].y1, 0, 1
622 }};
623 weston_matrix_transform(matrix, &vec1);
624 vec1.f[0] /= vec1.f[3];
625 vec1.f[1] /= vec1.f[3];
626
627 struct weston_vector vec2 = {{
628 src_rects[i].x2, src_rects[i].y2, 0, 1
629 }};
630 weston_matrix_transform(matrix, &vec2);
631 vec2.f[0] /= vec2.f[3];
632 vec2.f[1] /= vec2.f[3];
633
634 if (vec1.f[0] < vec2.f[0]) {
635 dest_rects[i].x1 = floor(vec1.f[0]);
636 dest_rects[i].x2 = ceil(vec2.f[0]);
637 } else {
638 dest_rects[i].x1 = floor(vec2.f[0]);
639 dest_rects[i].x2 = ceil(vec1.f[0]);
640 }
641
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600642 if (vec1.f[1] < vec2.f[1]) {
643 dest_rects[i].y1 = floor(vec1.f[1]);
644 dest_rects[i].y2 = ceil(vec2.f[1]);
645 } else {
646 dest_rects[i].y1 = floor(vec2.f[1]);
647 dest_rects[i].y2 = ceil(vec1.f[1]);
648 }
649 }
650
651 pixman_region32_clear(dest);
652 pixman_region32_init_rects(dest, dest_rects, nrects);
653 free(dest_rects);
654}
655
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200656WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500657weston_transformed_region(int width, int height,
658 enum wl_output_transform transform,
659 int32_t scale,
660 pixman_region32_t *src, pixman_region32_t *dest)
661{
662 pixman_box32_t *src_rects, *dest_rects;
663 int nrects, i;
664
665 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
666 if (src != dest)
667 pixman_region32_copy(dest, src);
668 return;
669 }
670
671 src_rects = pixman_region32_rectangles(src, &nrects);
672 dest_rects = malloc(nrects * sizeof(*dest_rects));
673 if (!dest_rects)
674 return;
675
676 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
677 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
678 } else {
679 for (i = 0; i < nrects; i++) {
680 switch (transform) {
681 default:
682 case WL_OUTPUT_TRANSFORM_NORMAL:
683 dest_rects[i].x1 = src_rects[i].x1;
684 dest_rects[i].y1 = src_rects[i].y1;
685 dest_rects[i].x2 = src_rects[i].x2;
686 dest_rects[i].y2 = src_rects[i].y2;
687 break;
688 case WL_OUTPUT_TRANSFORM_90:
689 dest_rects[i].x1 = height - src_rects[i].y2;
690 dest_rects[i].y1 = src_rects[i].x1;
691 dest_rects[i].x2 = height - src_rects[i].y1;
692 dest_rects[i].y2 = src_rects[i].x2;
693 break;
694 case WL_OUTPUT_TRANSFORM_180:
695 dest_rects[i].x1 = width - src_rects[i].x2;
696 dest_rects[i].y1 = height - src_rects[i].y2;
697 dest_rects[i].x2 = width - src_rects[i].x1;
698 dest_rects[i].y2 = height - src_rects[i].y1;
699 break;
700 case WL_OUTPUT_TRANSFORM_270:
701 dest_rects[i].x1 = src_rects[i].y1;
702 dest_rects[i].y1 = width - src_rects[i].x2;
703 dest_rects[i].x2 = src_rects[i].y2;
704 dest_rects[i].y2 = width - src_rects[i].x1;
705 break;
706 case WL_OUTPUT_TRANSFORM_FLIPPED:
707 dest_rects[i].x1 = width - src_rects[i].x2;
708 dest_rects[i].y1 = src_rects[i].y1;
709 dest_rects[i].x2 = width - src_rects[i].x1;
710 dest_rects[i].y2 = src_rects[i].y2;
711 break;
712 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
713 dest_rects[i].x1 = height - src_rects[i].y2;
714 dest_rects[i].y1 = width - src_rects[i].x2;
715 dest_rects[i].x2 = height - src_rects[i].y1;
716 dest_rects[i].y2 = width - src_rects[i].x1;
717 break;
718 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
719 dest_rects[i].x1 = src_rects[i].x1;
720 dest_rects[i].y1 = height - src_rects[i].y2;
721 dest_rects[i].x2 = src_rects[i].x2;
722 dest_rects[i].y2 = height - src_rects[i].y1;
723 break;
724 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
725 dest_rects[i].x1 = src_rects[i].y1;
726 dest_rects[i].y1 = src_rects[i].x1;
727 dest_rects[i].x2 = src_rects[i].y2;
728 dest_rects[i].y2 = src_rects[i].x2;
729 break;
730 }
731 }
732 }
733
734 if (scale != 1) {
735 for (i = 0; i < nrects; i++) {
736 dest_rects[i].x1 *= scale;
737 dest_rects[i].x2 *= scale;
738 dest_rects[i].y1 *= scale;
739 dest_rects[i].y2 *= scale;
740 }
741 }
742
743 pixman_region32_clear(dest);
744 pixman_region32_init_rects(dest, dest_rects, nrects);
745 free(dest_rects);
746}
747
Jonny Lamb74130762013-11-26 18:19:46 +0100748static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300749viewport_surface_to_buffer(struct weston_surface *surface,
750 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100751{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200752 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200753 double src_width, src_height;
754 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200755
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200756 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
757 if (vp->surface.width == -1) {
758 *bx = sx;
759 *by = sy;
760 return;
761 }
Jonny Lamb74130762013-11-26 18:19:46 +0100762
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200763 src_x = 0.0;
764 src_y = 0.0;
765 src_width = surface->width_from_buffer;
766 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100767 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200768 src_x = wl_fixed_to_double(vp->buffer.src_x);
769 src_y = wl_fixed_to_double(vp->buffer.src_y);
770 src_width = wl_fixed_to_double(vp->buffer.src_width);
771 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100772 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200773
774 *bx = sx * src_width / surface->width + src_x;
775 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100776}
777
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500778WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200779weston_surface_to_buffer_float(struct weston_surface *surface,
780 float sx, float sy, float *bx, float *by)
781{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200782 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
783
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300784 /* first transform coordinates if the viewport is set */
785 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100786
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500787 weston_transformed_coord(surface->width_from_buffer,
788 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200789 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100790 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200791}
792
Derek Foreman99739672015-12-03 16:38:11 -0600793/** Transform a rectangle from surface coordinates to buffer coordinates
794 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300795 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600796 * from.
797 * \param rect The rectangle to transform.
798 * \return The transformed rectangle.
799 *
800 * Viewport and buffer transformations can only do translation, scaling,
801 * and rotations in 90-degree steps. Therefore the only loss in the
802 * conversion is coordinate rounding.
803 *
804 * However, some coordinate rounding takes place as an intermediate
805 * step before the buffer scale factor is applied, so the rectangle
806 * boundary may not be exactly as expected.
807 *
808 * This is OK for damage tracking since a little extra coverage is
809 * not a problem.
810 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200811WL_EXPORT pixman_box32_t
812weston_surface_to_buffer_rect(struct weston_surface *surface,
813 pixman_box32_t rect)
814{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200815 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100816 float xf, yf;
817
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300818 /* first transform box coordinates if the viewport is set */
819 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100820 rect.x1 = floorf(xf);
821 rect.y1 = floorf(yf);
822
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300823 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600824 rect.x2 = ceilf(xf);
825 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100826
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500827 return weston_transformed_rect(surface->width_from_buffer,
828 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200829 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200830 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200831}
832
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200833/** Transform a region from surface coordinates to buffer coordinates
834 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300835 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200836 * from.
837 * \param surface_region[in] The region in surface coordinates.
838 * \param buffer_region[out] The region converted to buffer coordinates.
839 *
840 * Buffer_region must be init'd, but will be completely overwritten.
841 *
842 * Viewport and buffer transformations can only do translation, scaling,
843 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600844 * conversion is from the coordinate rounding that takes place in
845 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200846 */
847WL_EXPORT void
848weston_surface_to_buffer_region(struct weston_surface *surface,
849 pixman_region32_t *surface_region,
850 pixman_region32_t *buffer_region)
851{
852 pixman_box32_t *src_rects, *dest_rects;
853 int nrects, i;
854
855 src_rects = pixman_region32_rectangles(surface_region, &nrects);
856 dest_rects = malloc(nrects * sizeof(*dest_rects));
857 if (!dest_rects)
858 return;
859
860 for (i = 0; i < nrects; i++) {
861 dest_rects[i] = weston_surface_to_buffer_rect(surface,
862 src_rects[i]);
863 }
864
865 pixman_region32_fini(buffer_region);
866 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
867 free(dest_rects);
868}
869
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200870WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500871weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400872 struct weston_plane *plane)
873{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500874 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400875 return;
876
Jason Ekstranda7af7042013-10-12 22:38:11 -0500877 weston_view_damage_below(view);
878 view->plane = plane;
879 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400880}
881
Pekka Paalanen51723d52015-02-17 13:10:01 +0200882/** Inflict damage on the plane where the view is visible.
883 *
884 * \param view The view that causes the damage.
885 *
886 * If the view is currently on a plane (including the primary plane),
887 * take the view's boundingbox, subtract all the opaque views that cover it,
888 * and add the remaining region as damage to the plane. This corresponds
889 * to the damage inflicted to the plane if this view disappeared.
890 *
891 * A repaint is scheduled for this view.
892 *
893 * The region of all opaque views covering this view is stored in
894 * weston_view::clip and updated by view_accumulate_damage() during
895 * weston_output_repaint(). Specifically, that region matches the
896 * scenegraph as it was last painted.
897 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400898WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500899weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200900{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500901 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200902
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500903 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200904 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500905 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800906 if (view->plane)
907 pixman_region32_union(&view->plane->damage,
908 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500909 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800910 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200911}
912
Bryce Harrington3f650b82015-12-23 11:01:58 -0800913/**
914 * \param es The surface
915 * \param mask The new set of outputs for the surface
916 *
917 * Sets the surface's set of outputs to the ones specified by
918 * the new output mask provided. Identifies the outputs that
919 * have changed, the posts enter and leave events for these
920 * outputs as appropriate.
921 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400922static void
923weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
924{
925 uint32_t different = es->output_mask ^ mask;
926 uint32_t entered = mask & different;
927 uint32_t left = es->output_mask & different;
928 struct weston_output *output;
929 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500930 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400931
932 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500933 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400934 return;
935 if (different == 0)
936 return;
937
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500938 client = wl_resource_get_client(es->resource);
939
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400940 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800941 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400942 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500943 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400944 client);
945 if (resource == NULL)
946 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800947 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500948 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800949 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500950 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400951 }
952}
953
Bryce Harrington3f650b82015-12-23 11:01:58 -0800954/** Recalculate which output(s) the surface has views displayed on
955 *
956 * \param es The surface to remap to outputs
957 *
958 * Finds the output that is showing the largest amount of one
959 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300960 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800961 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300962 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800963 * in the output_mask for the surface.
964 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400965static void
966weston_surface_assign_output(struct weston_surface *es)
967{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 struct weston_output *new_output;
969 struct weston_view *view;
970 pixman_region32_t region;
971 uint32_t max, area, mask;
972 pixman_box32_t *e;
973
974 new_output = NULL;
975 max = 0;
976 mask = 0;
977 pixman_region32_init(&region);
978 wl_list_for_each(view, &es->views, surface_link) {
979 if (!view->output)
980 continue;
981
982 pixman_region32_intersect(&region, &view->transform.boundingbox,
983 &view->output->region);
984
985 e = pixman_region32_extents(&region);
986 area = (e->x2 - e->x1) * (e->y2 - e->y1);
987
988 mask |= view->output_mask;
989
990 if (area >= max) {
991 new_output = view->output;
992 max = area;
993 }
994 }
995 pixman_region32_fini(&region);
996
997 es->output = new_output;
998 weston_surface_update_output_mask(es, mask);
999}
1000
Bryce Harrington3f650b82015-12-23 11:01:58 -08001001/** Recalculate which output(s) the view is displayed on
1002 *
1003 * \param ev The view to remap to outputs
1004 *
1005 * Identifies the set of outputs that the view is visible on,
1006 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001007 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001008 *
1009 * Also does the same for the view's surface. See
1010 * weston_surface_assign_output().
1011 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012static void
1013weston_view_assign_output(struct weston_view *ev)
1014{
1015 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001016 struct weston_output *output, *new_output;
1017 pixman_region32_t region;
1018 uint32_t max, area, mask;
1019 pixman_box32_t *e;
1020
1021 new_output = NULL;
1022 max = 0;
1023 mask = 0;
1024 pixman_region32_init(&region);
1025 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001026 if (output->destroying)
1027 continue;
1028
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001030 &output->region);
1031
1032 e = pixman_region32_extents(&region);
1033 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1034
1035 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001036 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001037
1038 if (area >= max) {
1039 new_output = output;
1040 max = area;
1041 }
1042 }
1043 pixman_region32_fini(&region);
1044
Jason Ekstranda7af7042013-10-12 22:38:11 -05001045 ev->output = new_output;
1046 ev->output_mask = mask;
1047
1048 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001049}
1050
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001051static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001052weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1053 int from_x, int from_y, int *to_x, int *to_y)
1054{
1055 float x, y;
1056
1057 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1058 weston_view_from_global_float(to, x, y, &x, &y);
1059
1060 *to_x = round(x);
1061 *to_y = round(y);
1062}
1063
1064static void
1065weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1066{
1067 pixman_box32_t *a;
1068 pixman_box32_t b;
1069
1070 a = pixman_region32_extents(&from->geometry.scissor);
1071
1072 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1073 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1074
1075 pixman_region32_fini(&to->geometry.scissor);
1076 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1077}
1078
1079static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001080view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001081 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001082{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001083 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1084 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001085 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001086 { inbox->x1, inbox->y1 },
1087 { inbox->x1, inbox->y2 },
1088 { inbox->x2, inbox->y1 },
1089 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001090 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001091 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001092 int i;
1093
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001094 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001095 /* avoid rounding empty bbox to 1x1 */
1096 pixman_region32_init(bbox);
1097 return;
1098 }
1099
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001100 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001101 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001102 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001103 if (x < min_x)
1104 min_x = x;
1105 if (x > max_x)
1106 max_x = x;
1107 if (y < min_y)
1108 min_y = y;
1109 if (y > max_y)
1110 max_y = y;
1111 }
1112
Pekka Paalanen219b9822012-02-08 15:38:37 +02001113 int_x = floorf(min_x);
1114 int_y = floorf(min_y);
1115 pixman_region32_init_rect(bbox, int_x, int_y,
1116 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001117}
1118
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001119static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001121{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001123
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001124 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 view->geometry.x = roundf(view->geometry.x);
1126 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001127
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001128 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001129 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1130 view->transform.position.matrix.d[12] = view->geometry.x;
1131 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001132
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001134
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 view->transform.inverse = view->transform.position.matrix;
1136 view->transform.inverse.d[12] = -view->geometry.x;
1137 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001138
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001140 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001141 view->surface->width,
1142 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001143 if (view->geometry.scissor_enabled)
1144 pixman_region32_intersect(&view->transform.boundingbox,
1145 &view->transform.boundingbox,
1146 &view->geometry.scissor);
1147
1148 pixman_region32_translate(&view->transform.boundingbox,
1149 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001150
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 if (view->alpha == 1.0) {
1152 pixman_region32_copy(&view->transform.opaque,
1153 &view->surface->opaque);
1154 pixman_region32_translate(&view->transform.opaque,
1155 view->geometry.x,
1156 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001157 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001158}
1159
1160static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001162{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 struct weston_view *parent = view->geometry.parent;
1164 struct weston_matrix *matrix = &view->transform.matrix;
1165 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001166 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001167 pixman_region32_t surfregion;
1168 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001171
1172 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1174 view->transform.position.matrix.d[12] = view->geometry.x;
1175 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001176
1177 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001179 weston_matrix_multiply(matrix, &tform->matrix);
1180
Pekka Paalanen483243f2013-03-08 14:56:50 +02001181 if (parent)
1182 weston_matrix_multiply(matrix, &parent->transform.matrix);
1183
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001184 if (weston_matrix_invert(inverse, matrix) < 0) {
1185 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 weston_log("error: weston_view %p"
1187 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001188 return -1;
1189 }
1190
Pekka Paalanen380adf52015-02-16 14:39:11 +02001191 pixman_region32_init_rect(&surfregion, 0, 0,
1192 view->surface->width, view->surface->height);
1193 if (view->geometry.scissor_enabled)
1194 pixman_region32_intersect(&surfregion, &surfregion,
1195 &view->geometry.scissor);
1196 surfbox = pixman_region32_extents(&surfregion);
1197
1198 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1199 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001200
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001201 return 0;
1202}
1203
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001204static struct weston_layer *
1205get_view_layer(struct weston_view *view)
1206{
1207 if (view->parent_view)
1208 return get_view_layer(view->parent_view);
1209 return view->layer_link.layer;
1210}
1211
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001212WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001214{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001216 struct weston_layer *layer;
1217 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001218
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001220 return;
1221
Pekka Paalanen483243f2013-03-08 14:56:50 +02001222 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001224
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001226
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001228
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 pixman_region32_fini(&view->transform.boundingbox);
1230 pixman_region32_fini(&view->transform.opaque);
1231 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001232
Pekka Paalanencd403622012-01-25 13:37:39 +02001233 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 if (view->geometry.transformation_list.next ==
1235 &view->transform.position.link &&
1236 view->geometry.transformation_list.prev ==
1237 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001238 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001240 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 if (weston_view_update_transform_enable(view) < 0)
1242 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001243 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001244
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001245 layer = get_view_layer(view);
1246 if (layer) {
1247 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001248 pixman_region32_intersect(&view->transform.boundingbox,
1249 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001250 pixman_region32_intersect(&view->transform.opaque,
1251 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001252 pixman_region32_fini(&mask);
1253 }
1254
Pekka Paalanen380adf52015-02-16 14:39:11 +02001255 if (parent) {
1256 if (parent->geometry.scissor_enabled) {
1257 view->geometry.scissor_enabled = true;
1258 weston_view_transfer_scissor(parent, view);
1259 } else {
1260 view->geometry.scissor_enabled = false;
1261 }
1262 }
1263
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001265
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 wl_signal_emit(&view->surface->compositor->transform_signal,
1269 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001270}
1271
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001272WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001274{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001276
1277 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 * The invariant: if view->geometry.dirty, then all views
1279 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001280 * Corollary: if not parent->geometry.dirty, then all ancestors
1281 * are not dirty.
1282 */
1283
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001285 return;
1286
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001288
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001290 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001292}
1293
1294WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295weston_view_to_global_fixed(struct weston_view *view,
1296 wl_fixed_t vx, wl_fixed_t vy,
1297 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001298{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001299 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 weston_view_to_global_float(view,
1302 wl_fixed_to_double(vx),
1303 wl_fixed_to_double(vy),
1304 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001305 *x = wl_fixed_from_double(xf);
1306 *y = wl_fixed_from_double(yf);
1307}
1308
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001309WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310weston_view_from_global_float(struct weston_view *view,
1311 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001312{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001314 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1315
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001317
1318 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001319 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001321 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 *vx = 0;
1323 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001324 return;
1325 }
1326
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327 *vx = v.f[0] / v.f[3];
1328 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001329 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 *vx = x - view->geometry.x;
1331 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001332 }
1333}
1334
1335WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336weston_view_from_global_fixed(struct weston_view *view,
1337 wl_fixed_t x, wl_fixed_t y,
1338 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001339{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001341
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 weston_view_from_global_float(view,
1343 wl_fixed_to_double(x),
1344 wl_fixed_to_double(y),
1345 &vxf, &vyf);
1346 *vx = wl_fixed_from_double(vxf);
1347 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001348}
1349
1350WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351weston_view_from_global(struct weston_view *view,
1352 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001353{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001355
Jason Ekstranda7af7042013-10-12 22:38:11 -05001356 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1357 *vx = floorf(vxf);
1358 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001359}
1360
Bryce Harrington3f650b82015-12-23 11:01:58 -08001361/**
1362 * \param surface The surface to be repainted
1363 *
1364 * Marks the output(s) that the surface is shown on as needing to be
1365 * repainted. See weston_output_schedule_repaint().
1366 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001367WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001368weston_surface_schedule_repaint(struct weston_surface *surface)
1369{
1370 struct weston_output *output;
1371
1372 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001373 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001374 weston_output_schedule_repaint(output);
1375}
1376
Bryce Harrington3f650b82015-12-23 11:01:58 -08001377/**
1378 * \param view The view to be repainted
1379 *
1380 * Marks the output(s) that the view is shown on as needing to be
1381 * repainted. See weston_output_schedule_repaint().
1382 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001383WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384weston_view_schedule_repaint(struct weston_view *view)
1385{
1386 struct weston_output *output;
1387
1388 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001389 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390 weston_output_schedule_repaint(output);
1391}
1392
Pekka Paalanene508ce62015-02-19 13:59:55 +02001393/**
1394 * XXX: This function does it the wrong way.
1395 * surface->damage is the damage from the client, and causes
1396 * surface_flush_damage() to copy pixels. No window management action can
1397 * cause damage to the client-provided content, warranting re-upload!
1398 *
1399 * Instead of surface->damage, this function should record the damage
1400 * with all the views for this surface to avoid extraneous texture
1401 * uploads.
1402 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001404weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001405{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001406 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 0, 0, surface->width,
1408 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001409
Kristian Høgsberg98238702012-08-03 16:29:12 -04001410 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001411}
1412
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001413WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001415{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001416 if (view->geometry.x == x && view->geometry.y == y)
1417 return;
1418
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 view->geometry.x = x;
1420 view->geometry.y = y;
1421 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001422}
1423
Pekka Paalanen483243f2013-03-08 14:56:50 +02001424static void
1425transform_parent_handle_parent_destroy(struct wl_listener *listener,
1426 void *data)
1427{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 struct weston_view *view =
1429 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001430 geometry.parent_destroy_listener);
1431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001433}
1434
1435WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001437 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001438{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001439 if (view->geometry.parent) {
1440 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1441 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001442
1443 if (!parent)
1444 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001445 }
1446
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001448
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001450 transform_parent_handle_parent_destroy;
1451 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001452 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001454 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001456 }
1457
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458 weston_view_geometry_dirty(view);
1459}
1460
Pekka Paalanen380adf52015-02-16 14:39:11 +02001461/** Set a clip mask rectangle on a view
1462 *
1463 * \param view The view to set the clip mask on.
1464 * \param x Top-left corner X coordinate of the clip rectangle.
1465 * \param y Top-left corner Y coordinate of the clip rectangle.
1466 * \param width Width of the clip rectangle, non-negative.
1467 * \param height Height of the clip rectangle, non-negative.
1468 *
1469 * A shell may set a clip mask rectangle on a view. Everything outside
1470 * the rectangle is cut away for input and output purposes: it is
1471 * not drawn and cannot be hit by hit-test based input like pointer
1472 * motion or touch-downs. Everything inside the rectangle will behave
1473 * normally. Clients are unaware of clipping.
1474 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001475 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001476 * mask rectangle does not affect the view position, the view is positioned
1477 * as it would be without a clip. The clip also does not change
1478 * weston_surface::width,height.
1479 *
1480 * The clip mask rectangle is part of transformation inheritance
1481 * (weston_view_set_transform_parent()). A clip set in the root of the
1482 * transformation inheritance tree will affect all views in the tree.
1483 * A clip can be set only on the root view. Attempting to set a clip
1484 * on view that has a transformation parent will fail. Assigning a parent
1485 * to a view that has a clip set will cause the clip to be forgotten.
1486 *
1487 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1488 * on the additional transformations in the child views. These transformations
1489 * may not rotate the coordinate axes, i.e., only translation and scaling
1490 * are allowed. Violating this restriction causes the clipping to malfunction.
1491 * Furthermore, using scaling may cause rounding errors in child clipping.
1492 *
1493 * The clip mask rectangle is not automatically adjusted based on
1494 * wl_surface.attach dx and dy arguments.
1495 *
1496 * A clip mask rectangle can be set only if the compositor capability
1497 * WESTON_CAP_VIEW_CLIP_MASK is present.
1498 *
1499 * This function sets the clip mask rectangle and schedules a repaint for
1500 * the view.
1501 */
1502WL_EXPORT void
1503weston_view_set_mask(struct weston_view *view,
1504 int x, int y, int width, int height)
1505{
1506 struct weston_compositor *compositor = view->surface->compositor;
1507
1508 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1509 weston_log("%s not allowed without capability!\n", __func__);
1510 return;
1511 }
1512
1513 if (view->geometry.parent) {
1514 weston_log("view %p has a parent, clip forbidden!\n", view);
1515 return;
1516 }
1517
1518 if (width < 0 || height < 0) {
1519 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1520 x, y, width, height);
1521 return;
1522 }
1523
1524 pixman_region32_fini(&view->geometry.scissor);
1525 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1526 view->geometry.scissor_enabled = true;
1527 weston_view_geometry_dirty(view);
1528 weston_view_schedule_repaint(view);
1529}
1530
1531/** Remove the clip mask from a view
1532 *
1533 * \param view The view to remove the clip mask from.
1534 *
1535 * Removed the clip mask rectangle and schedules a repaint.
1536 *
1537 * \sa weston_view_set_mask
1538 */
1539WL_EXPORT void
1540weston_view_set_mask_infinite(struct weston_view *view)
1541{
1542 view->geometry.scissor_enabled = false;
1543 weston_view_geometry_dirty(view);
1544 weston_view_schedule_repaint(view);
1545}
1546
Armin Krezović0da12b82016-06-30 06:04:33 +02001547/* Check if view should be displayed
1548 *
1549 * The indicator is set manually when assigning
1550 * a view to a surface.
1551 *
1552 * This needs reworking. See the thread starting at:
1553 *
1554 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1555 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001556WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557weston_view_is_mapped(struct weston_view *view)
1558{
Armin Krezović0da12b82016-06-30 06:04:33 +02001559 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001560}
1561
Armin Krezović0da12b82016-06-30 06:04:33 +02001562/* Check if a surface has a view assigned to it
1563 *
1564 * The indicator is set manually when mapping
1565 * a surface and creating a view for it.
1566 *
1567 * This needs to go. See the thread starting at:
1568 *
1569 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1570 *
1571 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001572WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001573weston_surface_is_mapped(struct weston_surface *surface)
1574{
Armin Krezović0da12b82016-06-30 06:04:33 +02001575 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001576}
1577
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001578static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001579surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001580{
1581 struct weston_view *view;
1582
1583 if (surface->width == width && surface->height == height)
1584 return;
1585
1586 surface->width = width;
1587 surface->height = height;
1588
1589 wl_list_for_each(view, &surface->views, surface_link)
1590 weston_view_geometry_dirty(view);
1591}
1592
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001593WL_EXPORT void
1594weston_surface_set_size(struct weston_surface *surface,
1595 int32_t width, int32_t height)
1596{
1597 assert(!surface->resource);
1598 surface_set_size(surface, width, height);
1599}
1600
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001601static int
1602fixed_round_up_to_int(wl_fixed_t f)
1603{
1604 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1605}
1606
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001607static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001608convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1609 int32_t width, int32_t height,
1610 uint32_t transform,
1611 int32_t scale)
1612{
1613 assert(scale > 0);
1614
1615 switch (transform) {
1616 case WL_OUTPUT_TRANSFORM_NORMAL:
1617 case WL_OUTPUT_TRANSFORM_180:
1618 case WL_OUTPUT_TRANSFORM_FLIPPED:
1619 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1620 *width_out = width / scale;
1621 *height_out = height / scale;
1622 break;
1623 case WL_OUTPUT_TRANSFORM_90:
1624 case WL_OUTPUT_TRANSFORM_270:
1625 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1626 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1627 *width_out = height / scale;
1628 *height_out = width / scale;
1629 break;
1630 default:
1631 assert(0 && "invalid transform");
1632 }
1633}
1634
1635static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001636weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001637{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001638 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001639
1640 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001641 surface->width_from_buffer = 0;
1642 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001643 return;
1644 }
1645
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001646 convert_size_by_transform_scale(&surface->width_from_buffer,
1647 &surface->height_from_buffer,
1648 surface->buffer_ref.buffer->width,
1649 surface->buffer_ref.buffer->height,
1650 vp->buffer.transform,
1651 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001652}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001653
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001654static void
1655weston_surface_update_size(struct weston_surface *surface)
1656{
1657 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1658 int32_t width, height;
1659
1660 width = surface->width_from_buffer;
1661 height = surface->height_from_buffer;
1662
1663 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001664 surface_set_size(surface,
1665 vp->surface.width, vp->surface.height);
1666 return;
1667 }
1668
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001669 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001670 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1671 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1672
1673 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001674 return;
1675 }
1676
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001677 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001678}
1679
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001680WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001681weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001682{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001683 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001684
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001685 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001686
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001687 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001688}
1689
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690WL_EXPORT struct weston_view *
1691weston_compositor_pick_view(struct weston_compositor *compositor,
1692 wl_fixed_t x, wl_fixed_t y,
1693 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001694{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001695 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001696 wl_fixed_t view_x, view_y;
1697 int view_ix, view_iy;
1698 int ix = wl_fixed_to_int(x);
1699 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001700
Jason Ekstranda7af7042013-10-12 22:38:11 -05001701 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001702 if (!pixman_region32_contains_point(
1703 &view->transform.boundingbox, ix, iy, NULL))
1704 continue;
1705
1706 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1707 view_ix = wl_fixed_to_int(view_x);
1708 view_iy = wl_fixed_to_int(view_y);
1709
1710 if (!pixman_region32_contains_point(&view->surface->input,
1711 view_ix, view_iy, NULL))
1712 continue;
1713
Pekka Paalanen380adf52015-02-16 14:39:11 +02001714 if (view->geometry.scissor_enabled &&
1715 !pixman_region32_contains_point(&view->geometry.scissor,
1716 view_ix, view_iy, NULL))
1717 continue;
1718
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001719 *vx = view_x;
1720 *vy = view_y;
1721 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001722 }
1723
Derek Foremanf9318d12015-05-11 15:40:11 -05001724 *vx = wl_fixed_from_int(-1000000);
1725 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001726 return NULL;
1727}
1728
1729static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001730weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001731{
Daniel Stone37816df2012-05-16 18:45:18 +01001732 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001733
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001734 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001735 return;
1736
Daniel Stone37816df2012-05-16 18:45:18 +01001737 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001738 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001739}
1740
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001741WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001742weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001743{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001744 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001745
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 if (!weston_view_is_mapped(view))
1747 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001748
Jason Ekstranda7af7042013-10-12 22:38:11 -05001749 weston_view_damage_below(view);
1750 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001751 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001752 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001753 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001754 wl_list_remove(&view->link);
1755 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001756 view->output_mask = 0;
1757 weston_surface_assign_output(view->surface);
1758
1759 if (weston_surface_is_mapped(view->surface))
1760 return;
1761
1762 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001763 struct weston_touch *touch = weston_seat_get_touch(seat);
1764 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1765 struct weston_keyboard *keyboard =
1766 weston_seat_get_keyboard(seat);
1767
1768 if (keyboard && keyboard->focus == view->surface)
1769 weston_keyboard_set_focus(keyboard, NULL);
1770 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001771 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001772 if (touch && touch->focus == view)
1773 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001774 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001776
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777WL_EXPORT void
1778weston_surface_unmap(struct weston_surface *surface)
1779{
1780 struct weston_view *view;
1781
Armin Krezovićf8486c32016-06-30 06:04:28 +02001782 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783 wl_list_for_each(view, &surface->views, surface_link)
1784 weston_view_unmap(view);
1785 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001786}
1787
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001788static void
1789weston_surface_reset_pending_buffer(struct weston_surface *surface)
1790{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001791 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001792 surface->pending.sx = 0;
1793 surface->pending.sy = 0;
1794 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001795 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001796}
1797
Jason Ekstranda7af7042013-10-12 22:38:11 -05001798WL_EXPORT void
1799weston_view_destroy(struct weston_view *view)
1800{
1801 wl_signal_emit(&view->destroy_signal, view);
1802
1803 assert(wl_list_empty(&view->geometry.child_list));
1804
1805 if (weston_view_is_mapped(view)) {
1806 weston_view_unmap(view);
1807 weston_compositor_build_view_list(view->surface->compositor);
1808 }
1809
1810 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001811 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812
1813 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001814 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001816 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001817
1818 weston_view_set_transform_parent(view, NULL);
1819
Jason Ekstranda7af7042013-10-12 22:38:11 -05001820 wl_list_remove(&view->surface_link);
1821
1822 free(view);
1823}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001824
1825WL_EXPORT void
1826weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001827{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001828 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001830
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001831 if (--surface->ref_count > 0)
1832 return;
1833
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001834 assert(surface->resource == NULL);
1835
Pekka Paalanenca790762015-04-17 14:23:38 +03001836 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001837
Pekka Paalanene67858b2013-04-25 13:57:42 +03001838 assert(wl_list_empty(&surface->subsurface_list_pending));
1839 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001840
Jason Ekstranda7af7042013-10-12 22:38:11 -05001841 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1842 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001843
Jason Ekstrand7b982072014-05-20 14:33:03 -05001844 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001845
Pekka Paalanende685b82012-12-04 15:58:12 +02001846 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001847
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001848 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001849 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001850 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001851
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001852 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001853 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001854
Pekka Paalanen133e4392014-09-23 22:08:46 -04001855 weston_presentation_feedback_discard_list(&surface->feedback_list);
1856
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001857 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001858}
1859
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001860static void
1861destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001862{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001863 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001864
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001865 assert(surface);
1866
Giulio Camuffo0d379742013-11-15 22:06:15 +01001867 /* Set the resource to NULL, since we don't want to leave a
1868 * dangling pointer if the surface was refcounted and survives
1869 * the weston_surface_destroy() call. */
1870 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001871
1872 if (surface->viewport_resource)
1873 wl_resource_set_user_data(surface->viewport_resource, NULL);
1874
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001875 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001876}
1877
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001878static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001879weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1880{
1881 struct weston_buffer *buffer =
1882 container_of(listener, struct weston_buffer, destroy_listener);
1883
1884 wl_signal_emit(&buffer->destroy_signal, buffer);
1885 free(buffer);
1886}
1887
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001888WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001889weston_buffer_from_resource(struct wl_resource *resource)
1890{
1891 struct weston_buffer *buffer;
1892 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001893
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001894 listener = wl_resource_get_destroy_listener(resource,
1895 weston_buffer_destroy_handler);
1896
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001897 if (listener)
1898 return container_of(listener, struct weston_buffer,
1899 destroy_listener);
1900
1901 buffer = zalloc(sizeof *buffer);
1902 if (buffer == NULL)
1903 return NULL;
1904
1905 buffer->resource = resource;
1906 wl_signal_init(&buffer->destroy_signal);
1907 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001908 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001909 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001910
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001911 return buffer;
1912}
1913
1914static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001915weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1916 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001917{
Pekka Paalanende685b82012-12-04 15:58:12 +02001918 struct weston_buffer_reference *ref =
1919 container_of(listener, struct weston_buffer_reference,
1920 destroy_listener);
1921
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001922 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001923 ref->buffer = NULL;
1924}
1925
1926WL_EXPORT void
1927weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001928 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001929{
1930 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001931 ref->buffer->busy_count--;
1932 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001933 assert(wl_resource_get_client(ref->buffer->resource));
1934 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001935 WL_BUFFER_RELEASE);
1936 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001937 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001938 }
1939
Pekka Paalanende685b82012-12-04 15:58:12 +02001940 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001941 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001942 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001943 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001944 }
1945
Pekka Paalanende685b82012-12-04 15:58:12 +02001946 ref->buffer = buffer;
1947 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1948}
1949
1950static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001951weston_surface_attach(struct weston_surface *surface,
1952 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001953{
1954 weston_buffer_reference(&surface->buffer_ref, buffer);
1955
Pekka Paalanena6421c42012-12-04 15:58:10 +02001956 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001957 if (weston_surface_is_mapped(surface))
1958 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001959 }
1960
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001961 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001962
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001963 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001964 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001965}
1966
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001967WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001968weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001969{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001970 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001971
1972 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001973 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001974}
1975
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001976WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001977weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001978{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001979 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001980
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001981 pixman_region32_union(&compositor->primary_plane.damage,
1982 &compositor->primary_plane.damage,
1983 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001984 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001985}
1986
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001987static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001988surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001989{
Pekka Paalanende685b82012-12-04 15:58:12 +02001990 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001991 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001992 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001993
Pekka Paalanenb5026542014-11-12 15:09:24 +02001994 if (weston_timeline_enabled_ &&
1995 pixman_region32_not_empty(&surface->damage))
1996 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1997 TLP_OUTPUT(surface->output), TLP_END);
1998
Jason Ekstrandef540082014-06-26 10:37:36 -07001999 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002000}
2001
2002static void
2003view_accumulate_damage(struct weston_view *view,
2004 pixman_region32_t *opaque)
2005{
2006 pixman_region32_t damage;
2007
2008 pixman_region32_init(&damage);
2009 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002010 pixman_box32_t *extents;
2011
Jason Ekstranda7af7042013-10-12 22:38:11 -05002012 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002013 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002014 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002015 pixman_region32_copy(&damage, &view->surface->damage);
2016 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002017 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002018 }
2019
Pekka Paalanen380adf52015-02-16 14:39:11 +02002020 pixman_region32_intersect(&damage, &damage,
2021 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002022 pixman_region32_subtract(&damage, &damage, opaque);
2023 pixman_region32_union(&view->plane->damage,
2024 &view->plane->damage, &damage);
2025 pixman_region32_fini(&damage);
2026 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002027 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002028}
2029
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002030static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002031compositor_accumulate_damage(struct weston_compositor *ec)
2032{
2033 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002034 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002035 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002036
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002037 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002038
2039 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002040 pixman_region32_copy(&plane->clip, &clip);
2041
2042 pixman_region32_init(&opaque);
2043
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044 wl_list_for_each(ev, &ec->view_list, link) {
2045 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002046 continue;
2047
Jason Ekstranda7af7042013-10-12 22:38:11 -05002048 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002049 }
2050
2051 pixman_region32_union(&clip, &clip, &opaque);
2052 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002053 }
2054
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002055 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002056
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002058 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002059
2060 wl_list_for_each(ev, &ec->view_list, link) {
2061 if (ev->surface->touched)
2062 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002063 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002064
2065 surface_flush_damage(ev->surface);
2066
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002067 /* Both the renderer and the backend have seen the buffer
2068 * by now. If renderer needs the buffer, it has its own
2069 * reference set. If the backend wants to keep the buffer
2070 * around for migrating the surface into a non-primary plane
2071 * later, keep_buffer is true. Otherwise, drop the core
2072 * reference now, and allow early buffer release. This enables
2073 * clients to use single-buffering.
2074 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002075 if (!ev->surface->keep_buffer)
2076 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002077 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002078}
2079
2080static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002081surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002082{
2083 struct weston_subsurface *sub;
2084
Pekka Paalanene67858b2013-04-25 13:57:42 +03002085 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002086 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002087 continue;
2088
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2090 wl_list_init(&sub->surface->views);
2091
2092 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002093 }
2094}
2095
2096static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002097surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002098{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002099 struct weston_subsurface *sub;
2100 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002101
Jason Ekstranda7af7042013-10-12 22:38:11 -05002102 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2103 if (sub->surface == surface)
2104 continue;
2105
George Kiagiadakised04d382014-06-13 18:10:26 +02002106 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2107 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002108 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002109 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002110
2111 surface_free_unused_subsurface_views(sub->surface);
2112 }
2113}
2114
2115static void
2116view_list_add_subsurface_view(struct weston_compositor *compositor,
2117 struct weston_subsurface *sub,
2118 struct weston_view *parent)
2119{
2120 struct weston_subsurface *child;
2121 struct weston_view *view = NULL, *iv;
2122
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002123 if (!weston_surface_is_mapped(sub->surface))
2124 return;
2125
Jason Ekstranda7af7042013-10-12 22:38:11 -05002126 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2127 if (iv->geometry.parent == parent) {
2128 view = iv;
2129 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002130 }
2131 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132
2133 if (view) {
2134 /* Put it back in the surface's list of views */
2135 wl_list_remove(&view->surface_link);
2136 wl_list_insert(&sub->surface->views, &view->surface_link);
2137 } else {
2138 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002139 weston_view_set_position(view,
2140 sub->position.x,
2141 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142 weston_view_set_transform_parent(view, parent);
2143 }
2144
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002145 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002146 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002147 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002148
Pekka Paalanenb188e912013-11-19 14:03:35 +02002149 if (wl_list_empty(&sub->surface->subsurface_list)) {
2150 wl_list_insert(compositor->view_list.prev, &view->link);
2151 return;
2152 }
2153
2154 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2155 if (child->surface == sub->surface)
2156 wl_list_insert(compositor->view_list.prev, &view->link);
2157 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002158 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002159 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002160}
2161
2162static void
2163view_list_add(struct weston_compositor *compositor,
2164 struct weston_view *view)
2165{
2166 struct weston_subsurface *sub;
2167
2168 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169
Pekka Paalanenb188e912013-11-19 14:03:35 +02002170 if (wl_list_empty(&view->surface->subsurface_list)) {
2171 wl_list_insert(compositor->view_list.prev, &view->link);
2172 return;
2173 }
2174
2175 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2176 if (sub->surface == view->surface)
2177 wl_list_insert(compositor->view_list.prev, &view->link);
2178 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002180 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181}
2182
2183static void
2184weston_compositor_build_view_list(struct weston_compositor *compositor)
2185{
2186 struct weston_view *view;
2187 struct weston_layer *layer;
2188
2189 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002190 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191 surface_stash_subsurface_views(view->surface);
2192
2193 wl_list_init(&compositor->view_list);
2194 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002195 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002196 view_list_add(compositor, view);
2197 }
2198 }
2199
2200 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002201 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002202 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002203}
2204
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002205static void
2206weston_output_take_feedback_list(struct weston_output *output,
2207 struct weston_surface *surface)
2208{
2209 struct weston_view *view;
2210 struct weston_presentation_feedback *feedback;
2211 uint32_t flags = 0xffffffff;
2212
2213 if (wl_list_empty(&surface->feedback_list))
2214 return;
2215
2216 /* All views must have the flag for the flag to survive. */
2217 wl_list_for_each(view, &surface->views, surface_link) {
2218 /* ignore views that are not on this output at all */
2219 if (view->output_mask & (1u << output->id))
2220 flags &= view->psf_flags;
2221 }
2222
2223 wl_list_for_each(feedback, &surface->feedback_list, link)
2224 feedback->psf_flags = flags;
2225
2226 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2227 wl_list_init(&surface->feedback_list);
2228}
2229
David Herrmann1edf44c2013-10-22 17:11:26 +02002230static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002231weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002232{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002233 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002234 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002235 struct weston_animation *animation, *next;
2236 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002237 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002238 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002239 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002240
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002241 if (output->destroying)
2242 return 0;
2243
Pekka Paalanenb5026542014-11-12 15:09:24 +02002244 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2245
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002246 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002247 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002248
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002249 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002250 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002251 } else {
2252 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002253 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002254 ev->psf_flags = 0;
2255 }
2256 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002257
Pekka Paalanene67858b2013-04-25 13:57:42 +03002258 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002259 wl_list_for_each(ev, &ec->view_list, link) {
2260 /* Note: This operation is safe to do multiple times on the
2261 * same surface.
2262 */
2263 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002264 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002265 &ev->surface->frame_callback_list);
2266 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002267
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002268 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002269 }
2270 }
2271
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002272 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002273
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002274 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002275 pixman_region32_intersect(&output_damage,
2276 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002277 pixman_region32_subtract(&output_damage,
2278 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002279
Scott Moreauccbf29d2012-02-22 14:21:41 -07002280 if (output->dirty)
2281 weston_output_update_matrix(output);
2282
David Herrmann1edf44c2013-10-22 17:11:26 +02002283 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002284
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002285 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002286
Kristian Høgsbergef044142011-06-21 15:02:12 -04002287 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002288
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002289 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002290
Jonas Ådahldb773762012-06-13 00:01:21 +02002291 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002292 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002293 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002294 }
2295
Scott Moreaud64cf212012-06-08 19:40:54 -06002296 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002297 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002298 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002299 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002300
Pekka Paalanenb5026542014-11-12 15:09:24 +02002301 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2302
David Herrmann1edf44c2013-10-22 17:11:26 +02002303 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002304}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002305
Pekka Paalanen82919792014-05-21 13:51:49 +03002306static void
2307weston_output_schedule_repaint_reset(struct weston_output *output)
2308{
Pekka Paalanen82919792014-05-21 13:51:49 +03002309 output->repaint_scheduled = 0;
2310 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002311}
2312
Pekka Paalanen0513a952014-05-21 16:17:27 +03002313static int
2314output_repaint_timer_handler(void *data)
2315{
2316 struct weston_output *output = data;
2317 struct weston_compositor *compositor = output->compositor;
2318
2319 if (output->repaint_needed &&
2320 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2321 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2322 weston_output_repaint(output) == 0)
2323 return 0;
2324
2325 weston_output_schedule_repaint_reset(output);
2326
2327 return 0;
2328}
2329
Kristian Høgsbergef044142011-06-21 15:02:12 -04002330WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002331weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002332 const struct timespec *stamp,
2333 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002334{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002335 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002336 int32_t refresh_nsec;
2337 struct timespec now;
2338 struct timespec gone;
2339 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002340
Pekka Paalanenb5026542014-11-12 15:09:24 +02002341 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2342 TLP_VBLANK(stamp), TLP_END);
2343
Pekka Paalanend7894d02015-07-03 15:08:53 +03002344 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002345 weston_presentation_feedback_present_list(&output->feedback_list,
2346 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002347 output->msc,
2348 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002349
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002350 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002351
Pekka Paalanen0513a952014-05-21 16:17:27 +03002352 weston_compositor_read_presentation_clock(compositor, &now);
2353 timespec_sub(&gone, &now, stamp);
2354 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2355 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002356
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002357 if (msec < -1000 || msec > 1000) {
2358 static bool warned;
2359
2360 if (!warned)
2361 weston_log("Warning: computed repaint delay is "
2362 "insane: %d msec\n", msec);
2363 warned = true;
2364
2365 msec = 0;
2366 }
2367
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002368 /* Called from restart_repaint_loop and restart happens already after
2369 * the deadline given by repaint_msec? In that case we delay until
2370 * the deadline of the next frame, to give clients a more predictable
2371 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002372 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002373 msec += refresh_nsec / 1000000;
2374
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002375 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002376 output_repaint_timer_handler(output);
2377 else
2378 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002379}
2380
2381static void
2382idle_repaint(void *data)
2383{
2384 struct weston_output *output = data;
2385
Jonas Ådahle5a12252013-04-05 23:07:11 +02002386 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002387}
2388
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002389WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002390weston_layer_entry_insert(struct weston_layer_entry *list,
2391 struct weston_layer_entry *entry)
2392{
2393 wl_list_insert(&list->link, &entry->link);
2394 entry->layer = list->layer;
2395}
2396
2397WL_EXPORT void
2398weston_layer_entry_remove(struct weston_layer_entry *entry)
2399{
2400 wl_list_remove(&entry->link);
2401 wl_list_init(&entry->link);
2402 entry->layer = NULL;
2403}
2404
2405WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002406weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2407{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002408 wl_list_init(&layer->view_list.link);
2409 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002410 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002411 if (below != NULL)
2412 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002413}
2414
2415WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002416weston_layer_set_mask(struct weston_layer *layer,
2417 int x, int y, int width, int height)
2418{
2419 struct weston_view *view;
2420
2421 layer->mask.x1 = x;
2422 layer->mask.x2 = x + width;
2423 layer->mask.y1 = y;
2424 layer->mask.y2 = y + height;
2425
2426 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2427 weston_view_geometry_dirty(view);
2428 }
2429}
2430
2431WL_EXPORT void
2432weston_layer_set_mask_infinite(struct weston_layer *layer)
2433{
2434 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2435 UINT32_MAX, UINT32_MAX);
2436}
2437
2438WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002439weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002440{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002441 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002442 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002443
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002444 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2445 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002446 return;
2447
Pekka Paalanenb5026542014-11-12 15:09:24 +02002448 if (!output->repaint_needed)
2449 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2450
Kristian Høgsbergef044142011-06-21 15:02:12 -04002451 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002452 output->repaint_needed = 1;
2453 if (output->repaint_scheduled)
2454 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002455
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002456 wl_event_loop_add_idle(loop, idle_repaint, output);
2457 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002458 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002459}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002460
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002461WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002462weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2463{
2464 struct weston_output *output;
2465
2466 wl_list_for_each(output, &compositor->output_list, link)
2467 weston_output_schedule_repaint(output);
2468}
2469
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002470static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002471surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002472{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002473 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002474}
2475
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002476static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002477surface_attach(struct wl_client *client,
2478 struct wl_resource *resource,
2479 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2480{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002481 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002482 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002483
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002484 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002485 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002486 if (buffer == NULL) {
2487 wl_client_post_no_memory(client);
2488 return;
2489 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002490 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002491
Pekka Paalanende685b82012-12-04 15:58:12 +02002492 /* Attach, attach, without commit in between does not send
2493 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002494 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002495
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002496 surface->pending.sx = sx;
2497 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002498 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002499}
2500
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002501static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002502surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002503 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002504 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002505{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002506 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002507
Derek Foreman57e92ed2015-11-17 14:11:35 -06002508 if (width <= 0 || height <= 0)
2509 return;
2510
Derek Foreman152254b2015-11-26 14:17:48 -06002511 pixman_region32_union_rect(&surface->pending.damage_surface,
2512 &surface->pending.damage_surface,
2513 x, y, width, height);
2514}
2515
2516static void
2517surface_damage_buffer(struct wl_client *client,
2518 struct wl_resource *resource,
2519 int32_t x, int32_t y, int32_t width, int32_t height)
2520{
2521 struct weston_surface *surface = wl_resource_get_user_data(resource);
2522
2523 if (width <= 0 || height <= 0)
2524 return;
2525
2526 pixman_region32_union_rect(&surface->pending.damage_buffer,
2527 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002528 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002529}
2530
Kristian Høgsberg33418202011-08-16 23:01:28 -04002531static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002532destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002533{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002534 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002535
2536 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002537 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002538}
2539
2540static void
2541surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002542 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002543{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002544 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002545 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002546
2547 cb = malloc(sizeof *cb);
2548 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002549 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002550 return;
2551 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002552
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002553 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2554 callback);
2555 if (cb->resource == NULL) {
2556 free(cb);
2557 wl_resource_post_no_memory(resource);
2558 return;
2559 }
2560
Jason Ekstranda85118c2013-06-27 20:17:02 -05002561 wl_resource_set_implementation(cb->resource, NULL, cb,
2562 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002563
Pekka Paalanenbc106382012-10-10 12:49:31 +03002564 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002565}
2566
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002567static void
2568surface_set_opaque_region(struct wl_client *client,
2569 struct wl_resource *resource,
2570 struct wl_resource *region_resource)
2571{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002572 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002573 struct weston_region *region;
2574
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002575 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002576 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002577 pixman_region32_copy(&surface->pending.opaque,
2578 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002579 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002580 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002581 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002582}
2583
2584static void
2585surface_set_input_region(struct wl_client *client,
2586 struct wl_resource *resource,
2587 struct wl_resource *region_resource)
2588{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002589 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002590 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002591
2592 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002593 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002594 pixman_region32_copy(&surface->pending.input,
2595 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002596 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002597 pixman_region32_fini(&surface->pending.input);
2598 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002599 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002600}
2601
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002602static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002603weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002604{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002605 struct weston_subsurface *sub;
2606
2607 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2608 parent_link_pending) {
2609 wl_list_remove(&sub->parent_link);
2610 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2611 }
2612}
2613
2614static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002615weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002616 struct weston_matrix *matrix)
2617{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002618 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002619 double src_width, src_height, dest_width, dest_height;
2620
2621 weston_matrix_init(matrix);
2622
2623 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2624 src_width = surface->width_from_buffer;
2625 src_height = surface->height_from_buffer;
2626 } else {
2627 src_width = wl_fixed_to_double(vp->buffer.src_width);
2628 src_height = wl_fixed_to_double(vp->buffer.src_height);
2629 }
2630
2631 if (vp->surface.width == -1) {
2632 dest_width = src_width;
2633 dest_height = src_height;
2634 } else {
2635 dest_width = vp->surface.width;
2636 dest_height = vp->surface.height;
2637 }
2638
2639 if (src_width != dest_width || src_height != dest_height)
2640 weston_matrix_scale(matrix,
2641 src_width / dest_width,
2642 src_height / dest_height, 1);
2643
2644 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2645 weston_matrix_translate(matrix,
2646 wl_fixed_to_double(vp->buffer.src_x),
2647 wl_fixed_to_double(vp->buffer.src_y),
2648 0);
2649
2650 switch (vp->buffer.transform) {
2651 case WL_OUTPUT_TRANSFORM_FLIPPED:
2652 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2653 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2654 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2655 weston_matrix_scale(matrix, -1, 1, 1);
2656 weston_matrix_translate(matrix,
2657 surface->width_from_buffer, 0, 0);
2658 break;
2659 }
2660
2661 switch (vp->buffer.transform) {
2662 default:
2663 case WL_OUTPUT_TRANSFORM_NORMAL:
2664 case WL_OUTPUT_TRANSFORM_FLIPPED:
2665 break;
2666 case WL_OUTPUT_TRANSFORM_90:
2667 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2668 weston_matrix_rotate_xy(matrix, 0, 1);
2669 weston_matrix_translate(matrix,
2670 surface->height_from_buffer, 0, 0);
2671 break;
2672 case WL_OUTPUT_TRANSFORM_180:
2673 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2674 weston_matrix_rotate_xy(matrix, -1, 0);
2675 weston_matrix_translate(matrix,
2676 surface->width_from_buffer,
2677 surface->height_from_buffer, 0);
2678 break;
2679 case WL_OUTPUT_TRANSFORM_270:
2680 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2681 weston_matrix_rotate_xy(matrix, 0, -1);
2682 weston_matrix_translate(matrix,
2683 0, surface->width_from_buffer, 0);
2684 break;
2685 }
2686
2687 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2688}
2689
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002690/**
2691 * Compute a + b > c while being safe to overflows.
2692 */
2693static bool
2694fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2695{
2696 return (int64_t)a + (int64_t)b > (int64_t)c;
2697}
2698
2699static bool
2700weston_surface_is_pending_viewport_source_valid(
2701 const struct weston_surface *surface)
2702{
2703 const struct weston_surface_state *pend = &surface->pending;
2704 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2705 int width_from_buffer = 0;
2706 int height_from_buffer = 0;
2707 wl_fixed_t w;
2708 wl_fixed_t h;
2709
2710 /* If viewport source rect is not set, it is always ok. */
2711 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2712 return true;
2713
2714 if (pend->newly_attached) {
2715 if (pend->buffer) {
2716 convert_size_by_transform_scale(&width_from_buffer,
2717 &height_from_buffer,
2718 pend->buffer->width,
2719 pend->buffer->height,
2720 vp->buffer.transform,
2721 vp->buffer.scale);
2722 } else {
2723 /* No buffer: viewport is irrelevant. */
2724 return true;
2725 }
2726 } else {
2727 width_from_buffer = surface->width_from_buffer;
2728 height_from_buffer = surface->height_from_buffer;
2729 }
2730
2731 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2732 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2733
2734 /* No buffer: viewport is irrelevant. */
2735 if (width_from_buffer == 0 || height_from_buffer == 0)
2736 return true;
2737
2738 /* overflow checks for wl_fixed_from_int() */
2739 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2740 return false;
2741 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2742 return false;
2743
2744 w = wl_fixed_from_int(width_from_buffer);
2745 h = wl_fixed_from_int(height_from_buffer);
2746
2747 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2748 return false;
2749 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2750 return false;
2751
2752 return true;
2753}
2754
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002755static bool
2756fixed_is_integer(wl_fixed_t v)
2757{
2758 return (v & 0xff) == 0;
2759}
2760
2761static bool
2762weston_surface_is_pending_viewport_dst_size_int(
2763 const struct weston_surface *surface)
2764{
2765 const struct weston_buffer_viewport *vp =
2766 &surface->pending.buffer_viewport;
2767
2768 if (vp->surface.width != -1) {
2769 assert(vp->surface.width > 0 && vp->surface.height > 0);
2770 return true;
2771 }
2772
2773 return fixed_is_integer(vp->buffer.src_width) &&
2774 fixed_is_integer(vp->buffer.src_height);
2775}
2776
Derek Foreman152254b2015-11-26 14:17:48 -06002777/* Translate pending damage in buffer co-ordinates to surface
2778 * co-ordinates and union it with a pixman_region32_t.
2779 * This should only be called after the buffer is attached.
2780 */
2781static void
2782apply_damage_buffer(pixman_region32_t *dest,
2783 struct weston_surface *surface,
2784 struct weston_surface_state *state)
2785{
2786 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2787
2788 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2789 * translated into surface co-ordinates and unioned with
2790 * any other surface damage.
2791 * None of this makes sense if there is no buffer though.
2792 */
2793 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2794 pixman_region32_t buffer_damage;
2795
2796 pixman_region32_intersect_rect(&state->damage_buffer,
2797 &state->damage_buffer,
2798 0, 0, buffer->width,
2799 buffer->height);
2800 pixman_region32_init(&buffer_damage);
2801 weston_matrix_transform_region(&buffer_damage,
2802 &surface->buffer_to_surface_matrix,
2803 &state->damage_buffer);
2804 pixman_region32_union(dest, dest, &buffer_damage);
2805 pixman_region32_fini(&buffer_damage);
2806 }
2807 /* We should clear this on commit even if there was no buffer */
2808 pixman_region32_clear(&state->damage_buffer);
2809}
2810
Jason Ekstrand1e059042014-10-16 10:55:19 -05002811static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002812weston_surface_commit_state(struct weston_surface *surface,
2813 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002814{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002815 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002816 pixman_region32_t opaque;
2817
Alexander Larsson4ea95522013-05-22 14:41:37 +02002818 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002819 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002820 /* wp_viewport.set_source */
2821 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002822 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002823
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002824 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002825 if (state->newly_attached)
2826 weston_surface_attach(surface, state->buffer);
2827 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002828
Jason Ekstrand1e059042014-10-16 10:55:19 -05002829 weston_surface_build_buffer_matrix(surface,
2830 &surface->surface_to_buffer_matrix);
2831 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2832 &surface->surface_to_buffer_matrix);
2833
Jason Ekstrand7b982072014-05-20 14:33:03 -05002834 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002835 weston_surface_update_size(surface);
2836 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002837 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002838 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002839
Jason Ekstrand7b982072014-05-20 14:33:03 -05002840 state->sx = 0;
2841 state->sy = 0;
2842 state->newly_attached = 0;
2843 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002844
Derek Foreman152254b2015-11-26 14:17:48 -06002845 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002846 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002847 (pixman_region32_not_empty(&state->damage_surface) ||
2848 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002849 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002850
Pekka Paalanen8e159182012-10-10 12:49:25 +03002851 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002852 &state->damage_surface);
2853
2854 apply_damage_buffer(&surface->damage, surface, state);
2855
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002856 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002857 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002858 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002859
2860 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002861 pixman_region32_init(&opaque);
2862 pixman_region32_intersect_rect(&opaque, &state->opaque,
2863 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002864
2865 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2866 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002867 wl_list_for_each(view, &surface->views, surface_link)
2868 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002869 }
2870
2871 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002872
2873 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002874 pixman_region32_intersect_rect(&surface->input, &state->input,
2875 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002876
Pekka Paalanenbc106382012-10-10 12:49:31 +03002877 /* wl_surface.frame */
2878 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002879 &state->frame_callback_list);
2880 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002881
2882 /* XXX:
2883 * What should happen with a feedback request, if there
2884 * is no wl_buffer attached for this commit?
2885 */
2886
2887 /* presentation.feedback */
2888 wl_list_insert_list(&surface->feedback_list,
2889 &state->feedback_list);
2890 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002891}
2892
2893static void
2894weston_surface_commit(struct weston_surface *surface)
2895{
2896 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002897
Pekka Paalanene67858b2013-04-25 13:57:42 +03002898 weston_surface_commit_subsurface_order(surface);
2899
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002900 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002901}
2902
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002903static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002904weston_subsurface_commit(struct weston_subsurface *sub);
2905
2906static void
2907weston_subsurface_parent_commit(struct weston_subsurface *sub,
2908 int parent_is_synchronized);
2909
2910static void
2911surface_commit(struct wl_client *client, struct wl_resource *resource)
2912{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002913 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002914 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2915
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002916 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2917 assert(surface->viewport_resource);
2918
2919 wl_resource_post_error(surface->viewport_resource,
2920 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2921 "wl_surface@%d has viewport source outside buffer",
2922 wl_resource_get_id(resource));
2923 return;
2924 }
2925
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002926 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
2927 assert(surface->viewport_resource);
2928
2929 wl_resource_post_error(surface->viewport_resource,
2930 WP_VIEWPORT_ERROR_BAD_SIZE,
2931 "wl_surface@%d viewport dst size not integer",
2932 wl_resource_get_id(resource));
2933 return;
2934 }
2935
Pekka Paalanene67858b2013-04-25 13:57:42 +03002936 if (sub) {
2937 weston_subsurface_commit(sub);
2938 return;
2939 }
2940
2941 weston_surface_commit(surface);
2942
2943 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2944 if (sub->surface != surface)
2945 weston_subsurface_parent_commit(sub, 0);
2946 }
2947}
2948
2949static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002950surface_set_buffer_transform(struct wl_client *client,
2951 struct wl_resource *resource, int transform)
2952{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002953 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002954
Jonny Lamba55f1392014-05-30 12:07:15 +02002955 /* if wl_output.transform grows more members this will need to be updated. */
2956 if (transform < 0 ||
2957 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2958 wl_resource_post_error(resource,
2959 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2960 "buffer transform must be a valid transform "
2961 "('%d' specified)", transform);
2962 return;
2963 }
2964
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002965 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002966 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002967}
2968
Alexander Larsson4ea95522013-05-22 14:41:37 +02002969static void
2970surface_set_buffer_scale(struct wl_client *client,
2971 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002972 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002973{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002974 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002975
Jonny Lamba55f1392014-05-30 12:07:15 +02002976 if (scale < 1) {
2977 wl_resource_post_error(resource,
2978 WL_SURFACE_ERROR_INVALID_SCALE,
2979 "buffer scale must be at least one "
2980 "('%d' specified)", scale);
2981 return;
2982 }
2983
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002984 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002985 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002986}
2987
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002988static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002989 surface_destroy,
2990 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002991 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002992 surface_frame,
2993 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002994 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002995 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002996 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06002997 surface_set_buffer_scale,
2998 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002999};
3000
3001static void
3002compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003003 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003004{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003005 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003006 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003007
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003008 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003009 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003010 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003011 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003012 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003013
Jason Ekstranda85118c2013-06-27 20:17:02 -05003014 surface->resource =
3015 wl_resource_create(client, &wl_surface_interface,
3016 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003017 if (surface->resource == NULL) {
3018 weston_surface_destroy(surface);
3019 wl_resource_post_no_memory(resource);
3020 return;
3021 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003022 wl_resource_set_implementation(surface->resource, &surface_interface,
3023 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003024
3025 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003026}
3027
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003028static void
3029destroy_region(struct wl_resource *resource)
3030{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003031 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003032
3033 pixman_region32_fini(&region->region);
3034 free(region);
3035}
3036
3037static void
3038region_destroy(struct wl_client *client, struct wl_resource *resource)
3039{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003040 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003041}
3042
3043static void
3044region_add(struct wl_client *client, struct wl_resource *resource,
3045 int32_t x, int32_t y, int32_t width, int32_t height)
3046{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003047 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003048
3049 pixman_region32_union_rect(&region->region, &region->region,
3050 x, y, width, height);
3051}
3052
3053static void
3054region_subtract(struct wl_client *client, struct wl_resource *resource,
3055 int32_t x, int32_t y, int32_t width, int32_t height)
3056{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003057 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003058 pixman_region32_t rect;
3059
3060 pixman_region32_init_rect(&rect, x, y, width, height);
3061 pixman_region32_subtract(&region->region, &region->region, &rect);
3062 pixman_region32_fini(&rect);
3063}
3064
3065static const struct wl_region_interface region_interface = {
3066 region_destroy,
3067 region_add,
3068 region_subtract
3069};
3070
3071static void
3072compositor_create_region(struct wl_client *client,
3073 struct wl_resource *resource, uint32_t id)
3074{
3075 struct weston_region *region;
3076
3077 region = malloc(sizeof *region);
3078 if (region == NULL) {
3079 wl_resource_post_no_memory(resource);
3080 return;
3081 }
3082
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003083 pixman_region32_init(&region->region);
3084
Jason Ekstranda85118c2013-06-27 20:17:02 -05003085 region->resource =
3086 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003087 if (region->resource == NULL) {
3088 free(region);
3089 wl_resource_post_no_memory(resource);
3090 return;
3091 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003092 wl_resource_set_implementation(region->resource, &region_interface,
3093 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003094}
3095
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003096static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003097 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003098 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003099};
3100
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003101static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003102weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3103{
3104 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003105
Jason Ekstrand7b982072014-05-20 14:33:03 -05003106 weston_surface_commit_state(surface, &sub->cached);
3107 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003108
3109 weston_surface_commit_subsurface_order(surface);
3110
3111 weston_surface_schedule_repaint(surface);
3112
Jason Ekstrand7b982072014-05-20 14:33:03 -05003113 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003114}
3115
3116static void
3117weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3118{
3119 struct weston_surface *surface = sub->surface;
3120
3121 /*
3122 * If this commit would cause the surface to move by the
3123 * attach(dx, dy) parameters, the old damage region must be
3124 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003125 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003126 */
Derek Foreman152254b2015-11-26 14:17:48 -06003127 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003128 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003129 pixman_region32_union(&sub->cached.damage_surface,
3130 &sub->cached.damage_surface,
3131 &surface->pending.damage_surface);
3132 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003133
3134 if (surface->pending.newly_attached) {
3135 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003136 weston_surface_state_set_buffer(&sub->cached,
3137 surface->pending.buffer);
3138 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003139 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003140 weston_presentation_feedback_discard_list(
3141 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003142 }
3143 sub->cached.sx += surface->pending.sx;
3144 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003145
Derek Foreman152254b2015-11-26 14:17:48 -06003146 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3147
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003148 sub->cached.buffer_viewport.changed |=
3149 surface->pending.buffer_viewport.changed;
3150 sub->cached.buffer_viewport.buffer =
3151 surface->pending.buffer_viewport.buffer;
3152 sub->cached.buffer_viewport.surface =
3153 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003154
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003155 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003156
3157 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3158
3159 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3160
3161 wl_list_insert_list(&sub->cached.frame_callback_list,
3162 &surface->pending.frame_callback_list);
3163 wl_list_init(&surface->pending.frame_callback_list);
3164
Pekka Paalanen133e4392014-09-23 22:08:46 -04003165 wl_list_insert_list(&sub->cached.feedback_list,
3166 &surface->pending.feedback_list);
3167 wl_list_init(&surface->pending.feedback_list);
3168
Jason Ekstrand7b982072014-05-20 14:33:03 -05003169 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003170}
3171
Derek Foreman280e7dd2014-10-03 13:13:42 -05003172static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003173weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3174{
3175 while (sub) {
3176 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003177 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003178
3179 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003180 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003181
3182 sub = weston_surface_to_subsurface(sub->parent);
3183 }
3184
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003185 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003186}
3187
3188static void
3189weston_subsurface_commit(struct weston_subsurface *sub)
3190{
3191 struct weston_surface *surface = sub->surface;
3192 struct weston_subsurface *tmp;
3193
3194 /* Recursive check for effectively synchronized. */
3195 if (weston_subsurface_is_synchronized(sub)) {
3196 weston_subsurface_commit_to_cache(sub);
3197 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003198 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003199 /* flush accumulated state from cache */
3200 weston_subsurface_commit_to_cache(sub);
3201 weston_subsurface_commit_from_cache(sub);
3202 } else {
3203 weston_surface_commit(surface);
3204 }
3205
3206 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3207 if (tmp->surface != surface)
3208 weston_subsurface_parent_commit(tmp, 0);
3209 }
3210 }
3211}
3212
3213static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003214weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003215{
3216 struct weston_surface *surface = sub->surface;
3217 struct weston_subsurface *tmp;
3218
Pekka Paalanene67858b2013-04-25 13:57:42 +03003219 /* From now on, commit_from_cache the whole sub-tree, regardless of
3220 * the synchronized mode of each child. This sub-surface or some
3221 * of its ancestors were synchronized, so we are synchronized
3222 * all the way down.
3223 */
3224
Jason Ekstrand7b982072014-05-20 14:33:03 -05003225 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003226 weston_subsurface_commit_from_cache(sub);
3227
3228 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3229 if (tmp->surface != surface)
3230 weston_subsurface_parent_commit(tmp, 1);
3231 }
3232}
3233
3234static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003235weston_subsurface_parent_commit(struct weston_subsurface *sub,
3236 int parent_is_synchronized)
3237{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003238 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003239 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003240 wl_list_for_each(view, &sub->surface->views, surface_link)
3241 weston_view_set_position(view,
3242 sub->position.x,
3243 sub->position.y);
3244
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003245 sub->position.set = 0;
3246 }
3247
3248 if (parent_is_synchronized || sub->synchronized)
3249 weston_subsurface_synchronized_commit(sub);
3250}
3251
Pekka Paalanen8274d902014-08-06 19:36:51 +03003252static int
3253subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3254{
3255 return snprintf(buf, len, "sub-surface");
3256}
3257
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003258static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003259subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003260{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003261 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003262
Jason Ekstranda7af7042013-10-12 22:38:11 -05003263 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003264 weston_view_set_position(view,
3265 view->geometry.x + dx,
3266 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003267
3268 /* No need to check parent mappedness, because if parent is not
3269 * mapped, parent is not in a visible layer, so this sub-surface
3270 * will not be drawn either.
3271 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003272
Pekka Paalanene67858b2013-04-25 13:57:42 +03003273 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003274 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003275
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003276 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003277 * because that would call it also for the parent surface,
3278 * which might not be mapped yet. That would lead to
3279 * inconsistent state, where the window could never be
3280 * mapped.
3281 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003282 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003283 * weston_surface_is_mapped() return true, so that when the
3284 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003285 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003286 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003287 }
3288}
3289
3290static struct weston_subsurface *
3291weston_surface_to_subsurface(struct weston_surface *surface)
3292{
3293 if (surface->configure == subsurface_configure)
3294 return surface->configure_private;
3295
3296 return NULL;
3297}
3298
Pekka Paalanen01388e22013-04-25 13:57:44 +03003299WL_EXPORT struct weston_surface *
3300weston_surface_get_main_surface(struct weston_surface *surface)
3301{
3302 struct weston_subsurface *sub;
3303
3304 while (surface && (sub = weston_surface_to_subsurface(surface)))
3305 surface = sub->parent;
3306
3307 return surface;
3308}
3309
Pekka Paalanen50b67472014-10-01 15:02:41 +03003310WL_EXPORT int
3311weston_surface_set_role(struct weston_surface *surface,
3312 const char *role_name,
3313 struct wl_resource *error_resource,
3314 uint32_t error_code)
3315{
3316 assert(role_name);
3317
3318 if (surface->role_name == NULL ||
3319 surface->role_name == role_name ||
3320 strcmp(surface->role_name, role_name) == 0) {
3321 surface->role_name = role_name;
3322
3323 return 0;
3324 }
3325
3326 wl_resource_post_error(error_resource, error_code,
3327 "Cannot assign role %s to wl_surface@%d,"
3328 " already has role %s\n",
3329 role_name,
3330 wl_resource_get_id(surface->resource),
3331 surface->role_name);
3332 return -1;
3333}
3334
Pekka Paalanen8274d902014-08-06 19:36:51 +03003335WL_EXPORT void
3336weston_surface_set_label_func(struct weston_surface *surface,
3337 int (*desc)(struct weston_surface *,
3338 char *, size_t))
3339{
3340 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003341 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003342}
3343
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003344/** Get the size of surface contents
3345 *
3346 * \param surface The surface to query.
3347 * \param width Returns the width of raw contents.
3348 * \param height Returns the height of raw contents.
3349 *
3350 * Retrieves the raw surface content size in pixels for the given surface.
3351 * This is the whole content size in buffer pixels. If the surface
3352 * has no content or the renderer does not implement this feature,
3353 * zeroes are returned.
3354 *
3355 * This function is used to determine the buffer size needed for
3356 * a weston_surface_copy_content() call.
3357 */
3358WL_EXPORT void
3359weston_surface_get_content_size(struct weston_surface *surface,
3360 int *width, int *height)
3361{
3362 struct weston_renderer *rer = surface->compositor->renderer;
3363
3364 if (!rer->surface_get_content_size) {
3365 *width = 0;
3366 *height = 0;
3367 return;
3368 }
3369
3370 rer->surface_get_content_size(surface, width, height);
3371}
3372
3373/** Copy surface contents to system memory.
3374 *
3375 * \param surface The surface to copy from.
3376 * \param target Pointer to the target memory buffer.
3377 * \param size Size of the target buffer in bytes.
3378 * \param src_x X location on contents to copy from.
3379 * \param src_y Y location on contents to copy from.
3380 * \param width Width in pixels of the area to copy.
3381 * \param height Height in pixels of the area to copy.
3382 * \return 0 for success, -1 for failure.
3383 *
3384 * Surface contents are maintained by the renderer. They can be in a
3385 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3386 * else.
3387 *
3388 * Surface contents are copied into memory pointed to by target,
3389 * which has size bytes of space available. The target memory
3390 * may be larger than needed, but being smaller returns an error.
3391 * The extra bytes in target may or may not be written; their content is
3392 * unspecified. Size must be large enough to hold the image.
3393 *
3394 * The image in the target memory will be arranged in rows from
3395 * top to bottom, and pixels on a row from left to right. The pixel
3396 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3397 * width * 4.
3398 *
3399 * Parameters src_x and src_y define the upper-left corner in buffer
3400 * coordinates (pixels) to copy from. Parameters width and height
3401 * define the size of the area to copy in pixels.
3402 *
3403 * The rectangle defined by src_x, src_y, width, height must fit in
3404 * the surface contents. Otherwise an error is returned.
3405 *
3406 * Use surface_get_data_size to determine the content size; the
3407 * needed target buffer size and rectangle limits.
3408 *
3409 * CURRENT IMPLEMENTATION RESTRICTIONS:
3410 * - the machine must be little-endian due to Pixman formats.
3411 *
3412 * NOTE: Pixman formats are premultiplied.
3413 */
3414WL_EXPORT int
3415weston_surface_copy_content(struct weston_surface *surface,
3416 void *target, size_t size,
3417 int src_x, int src_y,
3418 int width, int height)
3419{
3420 struct weston_renderer *rer = surface->compositor->renderer;
3421 int cw, ch;
3422 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3423
3424 if (!rer->surface_copy_content)
3425 return -1;
3426
3427 weston_surface_get_content_size(surface, &cw, &ch);
3428
3429 if (src_x < 0 || src_y < 0)
3430 return -1;
3431
3432 if (width <= 0 || height <= 0)
3433 return -1;
3434
3435 if (src_x + width > cw || src_y + height > ch)
3436 return -1;
3437
3438 if (width * bytespp * height > size)
3439 return -1;
3440
3441 return rer->surface_copy_content(surface, target, size,
3442 src_x, src_y, width, height);
3443}
3444
Pekka Paalanene67858b2013-04-25 13:57:42 +03003445static void
3446subsurface_set_position(struct wl_client *client,
3447 struct wl_resource *resource, int32_t x, int32_t y)
3448{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003449 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003450
3451 if (!sub)
3452 return;
3453
3454 sub->position.x = x;
3455 sub->position.y = y;
3456 sub->position.set = 1;
3457}
3458
3459static struct weston_subsurface *
3460subsurface_from_surface(struct weston_surface *surface)
3461{
3462 struct weston_subsurface *sub;
3463
3464 sub = weston_surface_to_subsurface(surface);
3465 if (sub)
3466 return sub;
3467
3468 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3469 if (sub->surface == surface)
3470 return sub;
3471
3472 return NULL;
3473}
3474
3475static struct weston_subsurface *
3476subsurface_sibling_check(struct weston_subsurface *sub,
3477 struct weston_surface *surface,
3478 const char *request)
3479{
3480 struct weston_subsurface *sibling;
3481
3482 sibling = subsurface_from_surface(surface);
3483
3484 if (!sibling) {
3485 wl_resource_post_error(sub->resource,
3486 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3487 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003488 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003489 return NULL;
3490 }
3491
3492 if (sibling->parent != sub->parent) {
3493 wl_resource_post_error(sub->resource,
3494 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3495 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003496 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003497 return NULL;
3498 }
3499
3500 return sibling;
3501}
3502
3503static void
3504subsurface_place_above(struct wl_client *client,
3505 struct wl_resource *resource,
3506 struct wl_resource *sibling_resource)
3507{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003508 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003509 struct weston_surface *surface =
3510 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003511 struct weston_subsurface *sibling;
3512
3513 if (!sub)
3514 return;
3515
3516 sibling = subsurface_sibling_check(sub, surface, "place_above");
3517 if (!sibling)
3518 return;
3519
3520 wl_list_remove(&sub->parent_link_pending);
3521 wl_list_insert(sibling->parent_link_pending.prev,
3522 &sub->parent_link_pending);
3523}
3524
3525static void
3526subsurface_place_below(struct wl_client *client,
3527 struct wl_resource *resource,
3528 struct wl_resource *sibling_resource)
3529{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003530 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003531 struct weston_surface *surface =
3532 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003533 struct weston_subsurface *sibling;
3534
3535 if (!sub)
3536 return;
3537
3538 sibling = subsurface_sibling_check(sub, surface, "place_below");
3539 if (!sibling)
3540 return;
3541
3542 wl_list_remove(&sub->parent_link_pending);
3543 wl_list_insert(&sibling->parent_link_pending,
3544 &sub->parent_link_pending);
3545}
3546
3547static void
3548subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3549{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003550 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551
3552 if (sub)
3553 sub->synchronized = 1;
3554}
3555
3556static void
3557subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3558{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003559 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003560
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003561 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003562 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003563
3564 /* If sub became effectively desynchronized, flush. */
3565 if (!weston_subsurface_is_synchronized(sub))
3566 weston_subsurface_synchronized_commit(sub);
3567 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003568}
3569
3570static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003571weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3572{
3573 wl_list_remove(&sub->parent_link);
3574 wl_list_remove(&sub->parent_link_pending);
3575 wl_list_remove(&sub->parent_destroy_listener.link);
3576 sub->parent = NULL;
3577}
3578
3579static void
3580weston_subsurface_destroy(struct weston_subsurface *sub);
3581
3582static void
3583subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3584{
3585 struct weston_subsurface *sub =
3586 container_of(listener, struct weston_subsurface,
3587 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003588 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003589
3590 /* The protocol object (wl_resource) is left inert. */
3591 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003592 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003593
3594 weston_subsurface_destroy(sub);
3595}
3596
3597static void
3598subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3599{
3600 struct weston_subsurface *sub =
3601 container_of(listener, struct weston_subsurface,
3602 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003603 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003604 assert(sub->surface != sub->parent);
3605
3606 if (weston_surface_is_mapped(sub->surface))
3607 weston_surface_unmap(sub->surface);
3608
3609 weston_subsurface_unlink_parent(sub);
3610}
3611
3612static void
3613subsurface_resource_destroy(struct wl_resource *resource)
3614{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003615 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003616
3617 if (sub)
3618 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003619}
3620
3621static void
3622subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3623{
3624 wl_resource_destroy(resource);
3625}
3626
3627static void
3628weston_subsurface_link_parent(struct weston_subsurface *sub,
3629 struct weston_surface *parent)
3630{
3631 sub->parent = parent;
3632 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003633 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003634 &sub->parent_destroy_listener);
3635
3636 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3637 wl_list_insert(&parent->subsurface_list_pending,
3638 &sub->parent_link_pending);
3639}
3640
3641static void
3642weston_subsurface_link_surface(struct weston_subsurface *sub,
3643 struct weston_surface *surface)
3644{
3645 sub->surface = surface;
3646 sub->surface_destroy_listener.notify =
3647 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003648 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003649 &sub->surface_destroy_listener);
3650}
3651
3652static void
3653weston_subsurface_destroy(struct weston_subsurface *sub)
3654{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003655 struct weston_view *view, *next;
3656
Pekka Paalanene67858b2013-04-25 13:57:42 +03003657 assert(sub->surface);
3658
3659 if (sub->resource) {
3660 assert(weston_surface_to_subsurface(sub->surface) == sub);
3661 assert(sub->parent_destroy_listener.notify ==
3662 subsurface_handle_parent_destroy);
3663
George Kiagiadakised04d382014-06-13 18:10:26 +02003664 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3665 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003666 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003667 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003668
Pekka Paalanene67858b2013-04-25 13:57:42 +03003669 if (sub->parent)
3670 weston_subsurface_unlink_parent(sub);
3671
Jason Ekstrand7b982072014-05-20 14:33:03 -05003672 weston_surface_state_fini(&sub->cached);
3673 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003674
3675 sub->surface->configure = NULL;
3676 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003677 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003678 } else {
3679 /* the dummy weston_subsurface for the parent itself */
3680 assert(sub->parent_destroy_listener.notify == NULL);
3681 wl_list_remove(&sub->parent_link);
3682 wl_list_remove(&sub->parent_link_pending);
3683 }
3684
3685 wl_list_remove(&sub->surface_destroy_listener.link);
3686 free(sub);
3687}
3688
3689static const struct wl_subsurface_interface subsurface_implementation = {
3690 subsurface_destroy,
3691 subsurface_set_position,
3692 subsurface_place_above,
3693 subsurface_place_below,
3694 subsurface_set_sync,
3695 subsurface_set_desync
3696};
3697
3698static struct weston_subsurface *
3699weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3700 struct weston_surface *parent)
3701{
3702 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003703 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003704
Bryce Harringtonde16d892014-11-20 22:21:57 -08003705 sub = zalloc(sizeof *sub);
3706 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003707 return NULL;
3708
Jason Ekstranda7af7042013-10-12 22:38:11 -05003709 wl_list_init(&sub->unused_views);
3710
Jason Ekstranda85118c2013-06-27 20:17:02 -05003711 sub->resource =
3712 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003713 if (!sub->resource) {
3714 free(sub);
3715 return NULL;
3716 }
3717
Jason Ekstranda85118c2013-06-27 20:17:02 -05003718 wl_resource_set_implementation(sub->resource,
3719 &subsurface_implementation,
3720 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003721 weston_subsurface_link_surface(sub, surface);
3722 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003723 weston_surface_state_init(&sub->cached);
3724 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003725 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003726
3727 return sub;
3728}
3729
3730/* Create a dummy subsurface for having the parent itself in its
3731 * sub-surface lists. Makes stacking order manipulation easy.
3732 */
3733static struct weston_subsurface *
3734weston_subsurface_create_for_parent(struct weston_surface *parent)
3735{
3736 struct weston_subsurface *sub;
3737
Bryce Harringtonde16d892014-11-20 22:21:57 -08003738 sub = zalloc(sizeof *sub);
3739 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003740 return NULL;
3741
3742 weston_subsurface_link_surface(sub, parent);
3743 sub->parent = parent;
3744 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3745 wl_list_insert(&parent->subsurface_list_pending,
3746 &sub->parent_link_pending);
3747
3748 return sub;
3749}
3750
3751static void
3752subcompositor_get_subsurface(struct wl_client *client,
3753 struct wl_resource *resource,
3754 uint32_t id,
3755 struct wl_resource *surface_resource,
3756 struct wl_resource *parent_resource)
3757{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003758 struct weston_surface *surface =
3759 wl_resource_get_user_data(surface_resource);
3760 struct weston_surface *parent =
3761 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003762 struct weston_subsurface *sub;
3763 static const char where[] = "get_subsurface: wl_subsurface@";
3764
3765 if (surface == parent) {
3766 wl_resource_post_error(resource,
3767 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3768 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003769 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003770 return;
3771 }
3772
3773 if (weston_surface_to_subsurface(surface)) {
3774 wl_resource_post_error(resource,
3775 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3776 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003777 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003778 return;
3779 }
3780
Pekka Paalanen50b67472014-10-01 15:02:41 +03003781 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3782 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003783 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003784
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003785 if (weston_surface_get_main_surface(parent) == surface) {
3786 wl_resource_post_error(resource,
3787 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3788 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003789 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003790 return;
3791 }
3792
Pekka Paalanene67858b2013-04-25 13:57:42 +03003793 /* make sure the parent is in its own list */
3794 if (wl_list_empty(&parent->subsurface_list)) {
3795 if (!weston_subsurface_create_for_parent(parent)) {
3796 wl_resource_post_no_memory(resource);
3797 return;
3798 }
3799 }
3800
3801 sub = weston_subsurface_create(id, surface, parent);
3802 if (!sub) {
3803 wl_resource_post_no_memory(resource);
3804 return;
3805 }
3806
3807 surface->configure = subsurface_configure;
3808 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003809 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003810}
3811
3812static void
3813subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3814{
3815 wl_resource_destroy(resource);
3816}
3817
3818static const struct wl_subcompositor_interface subcompositor_interface = {
3819 subcompositor_destroy,
3820 subcompositor_get_subsurface
3821};
3822
3823static void
3824bind_subcompositor(struct wl_client *client,
3825 void *data, uint32_t version, uint32_t id)
3826{
3827 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003828 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003829
Jason Ekstranda85118c2013-06-27 20:17:02 -05003830 resource =
3831 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003832 if (resource == NULL) {
3833 wl_client_post_no_memory(client);
3834 return;
3835 }
3836 wl_resource_set_implementation(resource, &subcompositor_interface,
3837 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003838}
3839
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003840/** Set a DPMS mode on all of the compositor's outputs
3841 *
3842 * \param compositor The compositor instance
3843 * \param state The DPMS state the outputs will be set to
3844 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003845static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003846weston_compositor_dpms(struct weston_compositor *compositor,
3847 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003848{
3849 struct weston_output *output;
3850
3851 wl_list_for_each(output, &compositor->output_list, link)
3852 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003853 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003854}
3855
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003856/** Restores the compositor to active status
3857 *
3858 * \param compositor The compositor instance
3859 *
3860 * If the compositor was in a sleeping mode, all outputs are powered
3861 * back on via DPMS. Otherwise if the compositor was inactive
3862 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3863 * signal will fire.
3864 *
3865 * Restarts the idle timer.
3866 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003867WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003868weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003869{
Neil Roberts8b62e202013-09-30 13:14:47 +01003870 uint32_t old_state = compositor->state;
3871
3872 /* The state needs to be changed before emitting the wake
3873 * signal because that may try to schedule a repaint which
3874 * will not work if the compositor is still sleeping */
3875 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3876
3877 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003878 case WESTON_COMPOSITOR_SLEEPING:
3879 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3880 /* fall through */
3881 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003882 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003883 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003884 /* fall through */
3885 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003886 wl_event_source_timer_update(compositor->idle_source,
3887 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003888 }
3889}
3890
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003891/** Turns off rendering and frame events for the compositor.
3892 *
3893 * \param compositor The compositor instance
3894 *
3895 * This is used for example to prevent further rendering while the
3896 * compositor is shutting down.
3897 *
3898 * \note When offscreen state is entered, outputs will be powered
3899 * back on if they were sleeping (in DPMS off mode), even though
3900 * no rendering will be performed.
3901 *
3902 * Stops the idle timer.
3903 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003904WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003905weston_compositor_offscreen(struct weston_compositor *compositor)
3906{
3907 switch (compositor->state) {
3908 case WESTON_COMPOSITOR_OFFSCREEN:
3909 return;
3910 case WESTON_COMPOSITOR_SLEEPING:
3911 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3912 /* fall through */
3913 default:
3914 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3915 wl_event_source_timer_update(compositor->idle_source, 0);
3916 }
3917}
3918
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003919/** Powers down all attached output devices
3920 *
3921 * \param compositor The compositor instance
3922 *
3923 * Causes rendering to the outputs to cease, and no frame events to be
3924 * sent. Only powers down the outputs if the compositor is not already
3925 * in sleep mode.
3926 *
3927 * Stops the idle timer.
3928 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003929WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003930weston_compositor_sleep(struct weston_compositor *compositor)
3931{
3932 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3933 return;
3934
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003935 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003936 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3937 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3938}
3939
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003940/** Sets compositor to idle mode
3941 *
3942 * \param data The compositor instance
3943 *
3944 * This is called when the idle timer fires. Once the compositor is in
3945 * idle mode it requires a wake action (e.g. via
3946 * weston_compositor_wake()) to restore it. The compositor's
3947 * idle_signal will be triggered when the idle event occurs.
3948 *
3949 * Idleness can be inhibited by setting the compositor's idle_inhibit
3950 * property.
3951 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003952static int
3953idle_handler(void *data)
3954{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003955 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003956
3957 if (compositor->idle_inhibit)
3958 return 1;
3959
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003960 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003961 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003962
3963 return 1;
3964}
3965
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003966WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003967weston_plane_init(struct weston_plane *plane,
3968 struct weston_compositor *ec,
3969 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003970{
3971 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003972 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003973 plane->x = x;
3974 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003975 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003976
3977 /* Init the link so that the call to wl_list_remove() when releasing
3978 * the plane without ever stacking doesn't lead to a crash */
3979 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003980}
3981
3982WL_EXPORT void
3983weston_plane_release(struct weston_plane *plane)
3984{
Xiong Zhang97116532013-10-23 13:58:31 +08003985 struct weston_view *view;
3986
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003987 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003988 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003989
Xiong Zhang97116532013-10-23 13:58:31 +08003990 wl_list_for_each(view, &plane->compositor->view_list, link) {
3991 if (view->plane == plane)
3992 view->plane = NULL;
3993 }
3994
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003995 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003996}
3997
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003998WL_EXPORT void
3999weston_compositor_stack_plane(struct weston_compositor *ec,
4000 struct weston_plane *plane,
4001 struct weston_plane *above)
4002{
4003 if (above)
4004 wl_list_insert(above->link.prev, &plane->link);
4005 else
4006 wl_list_insert(&ec->plane_list, &plane->link);
4007}
4008
Casey Dahlin9074db52012-04-19 22:50:09 -04004009static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004010{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004011 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004012}
4013
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004014static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004015bind_output(struct wl_client *client,
4016 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004017{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004018 struct weston_output *output = data;
4019 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004020 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004021
Jason Ekstranda85118c2013-06-27 20:17:02 -05004022 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004023 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004024 if (resource == NULL) {
4025 wl_client_post_no_memory(client);
4026 return;
4027 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004028
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004029 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05004030 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004031
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004032 wl_output_send_geometry(resource,
4033 output->x,
4034 output->y,
4035 output->mm_width,
4036 output->mm_height,
4037 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004038 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004039 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004040 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004041 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004042 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004043
4044 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004045 wl_output_send_mode(resource,
4046 mode->flags,
4047 mode->width,
4048 mode->height,
4049 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004050 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004051
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004052 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004053 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004054}
4055
David Fort0de859e2016-05-27 23:22:57 +02004056/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004057static void
David Fort0de859e2016-05-27 23:22:57 +02004058weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4059 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004060{
4061 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004062 bool start_resizing = false;
4063
4064 if (!delta_width)
4065 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004066
4067 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004068 if (output == resized_output) {
4069 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004070 continue;
4071 }
4072
David Fort0de859e2016-05-27 23:22:57 +02004073 if (start_resizing) {
4074 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004075 output->dirty = 1;
4076 }
4077 }
4078}
4079
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004080WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004081weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004082{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004083 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004084 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004085
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004086 output->destroying = 1;
4087
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004088 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004089 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004090 weston_view_assign_output(view);
4091 }
4092
Pekka Paalanen0513a952014-05-21 16:17:27 +03004093 wl_event_source_remove(output->repaint_timer);
4094
Pekka Paalanen133e4392014-09-23 22:08:46 -04004095 weston_presentation_feedback_discard_list(&output->feedback_list);
4096
David Fort0de859e2016-05-27 23:22:57 +02004097 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004098 wl_list_remove(&output->link);
4099
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004100 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004101 wl_signal_emit(&output->destroy_signal, output);
4102
Richard Hughesafe690c2013-05-02 10:10:04 +01004103 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004104 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004105 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004106 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004107
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004108 wl_resource_for_each(resource, &output->resource_list) {
4109 wl_resource_set_destructor(resource, NULL);
4110 }
4111
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004112 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004113}
4114
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004115WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004116weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004117{
Scott Moreau850ca422012-05-21 15:21:25 -06004118 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004119
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004120 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004121 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004122
Scott Moreauccbf29d2012-02-22 14:21:41 -07004123 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004124 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004125 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004126 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004127 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004128 weston_matrix_scale(&output->matrix, magnification,
4129 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004130 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004131
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004132 switch (output->transform) {
4133 case WL_OUTPUT_TRANSFORM_FLIPPED:
4134 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4135 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4136 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4137 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4138 weston_matrix_scale(&output->matrix, -1, 1, 1);
4139 break;
4140 }
4141
4142 switch (output->transform) {
4143 default:
4144 case WL_OUTPUT_TRANSFORM_NORMAL:
4145 case WL_OUTPUT_TRANSFORM_FLIPPED:
4146 break;
4147 case WL_OUTPUT_TRANSFORM_90:
4148 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4149 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4150 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4151 break;
4152 case WL_OUTPUT_TRANSFORM_180:
4153 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4154 weston_matrix_translate(&output->matrix,
4155 -output->width, -output->height, 0);
4156 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4157 break;
4158 case WL_OUTPUT_TRANSFORM_270:
4159 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4160 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4161 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4162 break;
4163 }
4164
4165 if (output->current_scale != 1)
4166 weston_matrix_scale(&output->matrix,
4167 output->current_scale,
4168 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004169
Scott Moreauccbf29d2012-02-22 14:21:41 -07004170 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004171
4172 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004173}
4174
Scott Moreau1bad5db2012-08-18 01:04:05 -06004175static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004176weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004177{
4178 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004179 output->native_scale = scale;
4180 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004181
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004182 convert_size_by_transform_scale(&output->width, &output->height,
4183 output->current_mode->width,
4184 output->current_mode->height,
4185 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004186}
4187
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004188static void
4189weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004190{
4191 output->x = x;
4192 output->y = y;
4193
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004194 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004195 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004196 output->width,
4197 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004198}
4199
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004200WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004201weston_output_move(struct weston_output *output, int x, int y)
4202{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004203 struct wl_resource *resource;
4204
4205 output->move_x = x - output->x;
4206 output->move_y = y - output->y;
4207
4208 if (output->move_x == 0 && output->move_y == 0)
4209 return;
4210
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004211 weston_output_init_geometry(output, x, y);
4212
4213 output->dirty = 1;
4214
4215 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004216 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004217
4218 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004219 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004220 wl_output_send_geometry(resource,
4221 output->x,
4222 output->y,
4223 output->mm_width,
4224 output->mm_height,
4225 output->subpixel,
4226 output->make,
4227 output->model,
4228 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004229
FORT David8a120692016-04-26 23:34:06 +02004230 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004231 wl_output_send_done(resource);
4232 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004233}
4234
Bryce Harrington3f650b82015-12-23 11:01:58 -08004235/** Initialize a weston_output object's parameters
4236 *
4237 * \param output The weston_output object to initialize
4238 * \param c The output's compositor
4239 * \param x x coordinate for the output in global coordinate space
4240 * \param y y coordinate for the output in global coordinate space
4241 * \param mm_width Physical width of the output as reported by the backend
4242 * \param mm_height Physical height of the output as reported by the backend
4243 * \param transform Rotation of the output
4244 * \param scale Native scaling factor for the output
4245 *
4246 * Sets up the transformation, zoom, and geometry of the output using
4247 * the input properties.
4248 *
4249 * Establishes a repaint timer for the output with the relevant display
4250 * object's event loop. See output_repaint_timer_handler().
4251 *
4252 * The output is assigned an ID. Weston can support up to 32 distinct
4253 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4254 * is referred to and used to find the first available ID number, and
4255 * then this ID is marked as used in output_id_pool.
4256 *
4257 * The output is also assigned a Wayland global with the wl_output
4258 * external interface.
4259 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004260WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004261weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004262 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004263 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004264{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004265 struct wl_event_loop *loop;
4266
Bryce Harrington18e45732015-12-23 20:53:53 -08004267 /* Verify we haven't reached the limit of 32 available output IDs */
4268 assert(ffs(~c->output_id_pool) > 0);
4269
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004270 output->compositor = c;
4271 output->x = x;
4272 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004273 output->mm_width = mm_width;
4274 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004275 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004276 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004277
Alexander Larsson0b135062013-05-28 16:23:36 +02004278 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004279 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004280
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004281 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004282 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004283
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004284 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004285 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004286 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004287 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004288 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004289 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004290
Pekka Paalanen0513a952014-05-21 16:17:27 +03004291 loop = wl_display_get_event_loop(c->wl_display);
4292 output->repaint_timer = wl_event_loop_add_timer(loop,
4293 output_repaint_timer_handler, output);
4294
Bryce Harrington3f650b82015-12-23 11:01:58 -08004295 /* Invert the output id pool and look for the lowest numbered
4296 * switch (the least significant bit). Take that bit's position
4297 * as our ID, and mark it used in the compositor's output_id_pool.
4298 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004299 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004300 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004301
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004302 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004303 wl_global_create(c->wl_display, &wl_output_interface, 2,
4304 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004305}
4306
4307/** Adds an output to the compositor's output list and
4308 * send the compositor's output_created signal.
4309 *
4310 * \param compositor The compositor instance.
4311 * \param output The output to be added.
4312 */
4313WL_EXPORT void
4314weston_compositor_add_output(struct weston_compositor *compositor,
4315 struct weston_output *output)
4316{
4317 wl_list_insert(compositor->output_list.prev, &output->link);
4318 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004319}
4320
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004321WL_EXPORT void
4322weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004323 double device_x, double device_y,
4324 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004325{
Derek Foreman0f679412014-10-02 13:41:17 -05004326 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004327 device_x,
4328 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004329 0.0,
4330 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004331
Derek Foreman67a18b92015-03-24 11:36:14 -05004332 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004333
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004334 *x = p.f[0] / p.f[3];
4335 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004336}
4337
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004338static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004339destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004340{
Jonny Lamb74130762013-11-26 18:19:46 +01004341 struct weston_surface *surface =
4342 wl_resource_get_user_data(resource);
4343
Pekka Paalanen4826f872016-04-22 14:14:38 +03004344 if (!surface)
4345 return;
4346
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004347 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004348 surface->pending.buffer_viewport.buffer.src_width =
4349 wl_fixed_from_int(-1);
4350 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004351 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004352}
4353
4354static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004355viewport_destroy(struct wl_client *client,
4356 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004357{
4358 wl_resource_destroy(resource);
4359}
4360
4361static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004362viewport_set_source(struct wl_client *client,
4363 struct wl_resource *resource,
4364 wl_fixed_t src_x,
4365 wl_fixed_t src_y,
4366 wl_fixed_t src_width,
4367 wl_fixed_t src_height)
4368{
4369 struct weston_surface *surface =
4370 wl_resource_get_user_data(resource);
4371
Pekka Paalanen4826f872016-04-22 14:14:38 +03004372 if (!surface) {
4373 wl_resource_post_error(resource,
4374 WP_VIEWPORT_ERROR_NO_SURFACE,
4375 "wl_surface for this viewport is no longer exists");
4376 return;
4377 }
4378
4379 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004380 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004381
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004382 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004383 src_height == wl_fixed_from_int(-1) &&
4384 src_x == wl_fixed_from_int(-1) &&
4385 src_y == wl_fixed_from_int(-1)) {
4386 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004387 surface->pending.buffer_viewport.buffer.src_width =
4388 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004389 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004390 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004391 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004392
Pekka Paalanen201769a2016-04-26 14:42:11 +03004393 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004394 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004395 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004396 "wl_surface@%d viewport source "
4397 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4398 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004399 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004400 wl_fixed_to_double(src_height),
4401 wl_fixed_to_double(src_x),
4402 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004403 return;
4404 }
4405
4406 surface->pending.buffer_viewport.buffer.src_x = src_x;
4407 surface->pending.buffer_viewport.buffer.src_y = src_y;
4408 surface->pending.buffer_viewport.buffer.src_width = src_width;
4409 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004410 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004411}
4412
4413static void
4414viewport_set_destination(struct wl_client *client,
4415 struct wl_resource *resource,
4416 int32_t dst_width,
4417 int32_t dst_height)
4418{
4419 struct weston_surface *surface =
4420 wl_resource_get_user_data(resource);
4421
Pekka Paalanen4826f872016-04-22 14:14:38 +03004422 if (!surface) {
4423 wl_resource_post_error(resource,
4424 WP_VIEWPORT_ERROR_NO_SURFACE,
4425 "wl_surface for this viewport no longer exists");
4426 return;
4427 }
4428
4429 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004430
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004431 if (dst_width == -1 && dst_height == -1) {
4432 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004433 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004434 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004435 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004436 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004437
4438 if (dst_width <= 0 || dst_height <= 0) {
4439 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004440 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004441 "destination size must be positive (%dx%d)",
4442 dst_width, dst_height);
4443 return;
4444 }
4445
4446 surface->pending.buffer_viewport.surface.width = dst_width;
4447 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004448 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004449}
4450
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004451static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004452 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004453 viewport_set_source,
4454 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004455};
4456
4457static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004458viewporter_destroy(struct wl_client *client,
4459 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004460{
4461 wl_resource_destroy(resource);
4462}
4463
4464static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004465viewporter_get_viewport(struct wl_client *client,
4466 struct wl_resource *viewporter,
4467 uint32_t id,
4468 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004469{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004470 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004471 struct weston_surface *surface =
4472 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004473 struct wl_resource *resource;
4474
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004475 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004476 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004477 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004478 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004479 return;
4480 }
4481
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004482 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004483 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004484 if (resource == NULL) {
4485 wl_client_post_no_memory(client);
4486 return;
4487 }
4488
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004489 wl_resource_set_implementation(resource, &viewport_interface,
4490 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004491
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004492 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004493}
4494
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004495static const struct wp_viewporter_interface viewporter_interface = {
4496 viewporter_destroy,
4497 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004498};
4499
4500static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004501bind_viewporter(struct wl_client *client,
4502 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004503{
4504 struct wl_resource *resource;
4505
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004506 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004507 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004508 if (resource == NULL) {
4509 wl_client_post_no_memory(client);
4510 return;
4511 }
4512
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004513 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004514 NULL, NULL);
4515}
4516
4517static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004518destroy_presentation_feedback(struct wl_resource *feedback_resource)
4519{
4520 struct weston_presentation_feedback *feedback;
4521
4522 feedback = wl_resource_get_user_data(feedback_resource);
4523
4524 wl_list_remove(&feedback->link);
4525 free(feedback);
4526}
4527
4528static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004529presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4530{
4531 wl_resource_destroy(resource);
4532}
4533
4534static void
4535presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004536 struct wl_resource *presentation_resource,
4537 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004538 uint32_t callback)
4539{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004540 struct weston_surface *surface;
4541 struct weston_presentation_feedback *feedback;
4542
4543 surface = wl_resource_get_user_data(surface_resource);
4544
Bryce Harringtonde16d892014-11-20 22:21:57 -08004545 feedback = zalloc(sizeof *feedback);
4546 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004547 goto err_calloc;
4548
4549 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004550 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004551 1, callback);
4552 if (!feedback->resource)
4553 goto err_create;
4554
4555 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4556 destroy_presentation_feedback);
4557
4558 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4559
4560 return;
4561
4562err_create:
4563 free(feedback);
4564
4565err_calloc:
4566 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004567}
4568
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004569static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004570 presentation_destroy,
4571 presentation_feedback
4572};
4573
4574static void
4575bind_presentation(struct wl_client *client,
4576 void *data, uint32_t version, uint32_t id)
4577{
4578 struct weston_compositor *compositor = data;
4579 struct wl_resource *resource;
4580
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004581 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004582 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004583 if (resource == NULL) {
4584 wl_client_post_no_memory(client);
4585 return;
4586 }
4587
4588 wl_resource_set_implementation(resource, &presentation_implementation,
4589 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004590 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004591}
4592
4593static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004594compositor_bind(struct wl_client *client,
4595 void *data, uint32_t version, uint32_t id)
4596{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004597 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004598 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004599
Jason Ekstranda85118c2013-06-27 20:17:02 -05004600 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004601 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004602 if (resource == NULL) {
4603 wl_client_post_no_memory(client);
4604 return;
4605 }
4606
4607 wl_resource_set_implementation(resource, &compositor_interface,
4608 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004609}
4610
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004611WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004612weston_environment_get_fd(const char *env)
4613{
4614 char *e, *end;
4615 int fd, flags;
4616
4617 e = getenv(env);
4618 if (!e)
4619 return -1;
Bryce Harrington375759e2016-07-12 16:51:27 -07004620 fd = strtol(e, &end, 10);
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004621 if (*end != '\0')
4622 return -1;
4623
4624 flags = fcntl(fd, F_GETFD);
4625 if (flags == -1)
4626 return -1;
4627
4628 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4629 unsetenv(env);
4630
4631 return fd;
4632}
4633
Pekka Paalanenb5026542014-11-12 15:09:24 +02004634static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004635timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004636 uint32_t key, void *data)
4637{
4638 struct weston_compositor *compositor = data;
4639
4640 if (weston_timeline_enabled_)
4641 weston_timeline_close();
4642 else
4643 weston_timeline_open(compositor);
4644}
4645
Giulio Camuffo459137b2014-10-11 23:56:24 +03004646/** Create the compositor.
4647 *
4648 * This functions creates and initializes a compositor instance.
4649 *
4650 * \param display The Wayland display to be used.
4651 * \param user_data A pointer to an object that can later be retrieved
4652 * using the \ref weston_compositor_get_user_data function.
4653 * \return The compositor instance on success or NULL on failure.
4654 */
4655WL_EXPORT struct weston_compositor *
4656weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004657{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004658 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004659 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004660
Giulio Camuffo459137b2014-10-11 23:56:24 +03004661 ec = zalloc(sizeof *ec);
4662 if (!ec)
4663 return NULL;
4664
4665 ec->wl_display = display;
4666 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004667 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004668 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004669 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004670 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004671 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004672 wl_signal_init(&ec->idle_signal);
4673 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004674 wl_signal_init(&ec->show_input_panel_signal);
4675 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004676 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004677 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004678 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004679 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004680 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004681 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004682 wl_signal_init(&ec->session_signal);
4683 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004684
Casey Dahlin58ba1372012-04-19 22:50:08 -04004685 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004686 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004687
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02004688 ec->activate_serial = 1;
4689
Derek Foreman152254b2015-11-26 14:17:48 -06004690 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004691 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004692 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004693
Giulio Camuffo954f1832014-10-11 18:27:30 +03004694 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004695 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004696 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004697
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004698 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004699 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004700 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004701
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004702 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004703 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004704 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004705
Jason Ekstranda7af7042013-10-12 22:38:11 -05004706 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004707 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004708 wl_list_init(&ec->layer_list);
4709 wl_list_init(&ec->seat_list);
4710 wl_list_init(&ec->output_list);
4711 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004712 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004713 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004714 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004715 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004716 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004717
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004718 wl_list_init(&ec->plugin_api_list);
4719
Xiong Zhang97116532013-10-23 13:58:31 +08004720 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004721 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004722
Giulio Camuffo459137b2014-10-11 23:56:24 +03004723 wl_data_device_manager_init(ec->wl_display);
4724
4725 wl_display_init_shm(ec->wl_display);
4726
4727 loop = wl_display_get_event_loop(ec->wl_display);
4728 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004729
Giulio Camuffo459137b2014-10-11 23:56:24 +03004730 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4731 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4732
4733 weston_compositor_add_debug_binding(ec, KEY_T,
4734 timeline_key_binding_handler, ec);
4735
Giulio Camuffo459137b2014-10-11 23:56:24 +03004736 return ec;
4737
4738fail:
4739 free(ec);
4740 return NULL;
4741}
4742
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004743WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004744weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004745{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004746 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004747
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004748 wl_event_source_remove(ec->idle_source);
4749
Matt Roper361d2ad2011-08-29 13:52:23 -07004750 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004751 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004752 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004753
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004754 if (ec->renderer)
4755 ec->renderer->destroy(ec);
4756
Daniel Stone325fc2d2012-05-30 16:31:58 +01004757 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004758 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004759 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004760 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004761 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004762 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004763
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004764 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004765}
4766
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004767WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004768weston_compositor_exit_with_code(struct weston_compositor *compositor,
4769 int exit_code)
4770{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004771 if (compositor->exit_code == EXIT_SUCCESS)
4772 compositor->exit_code = exit_code;
4773
Giulio Camuffo459137b2014-10-11 23:56:24 +03004774 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004775}
4776
4777WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004778weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4779 const struct weston_pointer_grab_interface *interface)
4780{
4781 struct weston_seat *seat;
4782
4783 ec->default_pointer_grab = interface;
4784 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004785 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4786
4787 if (pointer)
4788 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004789 }
4790}
4791
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004792WL_EXPORT int
4793weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4794 clockid_t clk_id)
4795{
4796 struct timespec ts;
4797
4798 if (clock_gettime(clk_id, &ts) < 0)
4799 return -1;
4800
4801 compositor->presentation_clock = clk_id;
4802
4803 return 0;
4804}
4805
4806/*
4807 * For choosing the software clock, when the display hardware or API
4808 * does not expose a compatible presentation timestamp.
4809 */
4810WL_EXPORT int
4811weston_compositor_set_presentation_clock_software(
4812 struct weston_compositor *compositor)
4813{
4814 /* In order of preference */
4815 static const clockid_t clocks[] = {
4816 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4817 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4818 CLOCK_MONOTONIC, /* no jumps, may crawl */
4819 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4820 CLOCK_REALTIME /* may jump and crawl */
4821 };
4822 unsigned i;
4823
4824 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4825 if (weston_compositor_set_presentation_clock(compositor,
4826 clocks[i]) == 0)
4827 return 0;
4828
4829 weston_log("Error: no suitable presentation clock available.\n");
4830
4831 return -1;
4832}
4833
Pekka Paalanen662f3842015-03-18 12:17:26 +02004834/** Read the current time from the Presentation clock
4835 *
4836 * \param compositor
4837 * \param ts[out] The current time.
4838 *
4839 * \note Reading the current time in user space is always imprecise to some
4840 * degree.
4841 *
4842 * This function is never meant to fail. If reading the clock does fail,
4843 * an error message is logged and a zero time is returned. Callers are not
4844 * supposed to detect or react to failures.
4845 */
4846WL_EXPORT void
4847weston_compositor_read_presentation_clock(
4848 const struct weston_compositor *compositor,
4849 struct timespec *ts)
4850{
4851 static bool warned;
4852 int ret;
4853
4854 ret = clock_gettime(compositor->presentation_clock, ts);
4855 if (ret < 0) {
4856 ts->tv_sec = 0;
4857 ts->tv_nsec = 0;
4858
4859 if (!warned)
4860 weston_log("Error: failure to read "
4861 "the presentation clock %#x: '%m' (%d)\n",
4862 compositor->presentation_clock, errno);
4863 warned = true;
4864 }
4865}
4866
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004867/** Import dmabuf buffer into current renderer
4868 *
4869 * \param compositor
4870 * \param buffer the dmabuf buffer to import
4871 * \return true on usable buffers, false otherwise
4872 *
4873 * This function tests that the linux_dmabuf_buffer is usable
4874 * for the current renderer. Returns false on unusable buffers. Usually
4875 * usability is tested by importing the dmabufs for composition.
4876 *
4877 * This hook is also used for detecting if the renderer supports
4878 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4879 * supported.
4880 * */
4881WL_EXPORT bool
4882weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4883 struct linux_dmabuf_buffer *buffer)
4884{
4885 struct weston_renderer *renderer;
4886
4887 renderer = compositor->renderer;
4888
4889 if (renderer->import_dmabuf == NULL)
4890 return false;
4891
4892 return renderer->import_dmabuf(compositor, buffer);
4893}
4894
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004895WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004896weston_version(int *major, int *minor, int *micro)
4897{
4898 *major = WESTON_VERSION_MAJOR;
4899 *minor = WESTON_VERSION_MINOR;
4900 *micro = WESTON_VERSION_MICRO;
4901}
4902
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004903WL_EXPORT void *
4904weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004905{
Derek Foreman3f86e502015-06-08 11:46:54 -05004906 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004907 char path[PATH_MAX];
4908 void *module, *init;
4909
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004910 if (name == NULL)
4911 return NULL;
4912
Derek Foreman3f86e502015-06-08 11:46:54 -05004913 if (name[0] != '/') {
4914 if (builddir)
4915 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4916 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03004917 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004918 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004919 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004920 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004921
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004922 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4923 if (module) {
4924 weston_log("Module '%s' already loaded\n", path);
4925 dlclose(module);
4926 return NULL;
4927 }
4928
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004929 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004930 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004931 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004932 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004933 return NULL;
4934 }
4935
4936 init = dlsym(module, entrypoint);
4937 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004938 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004939 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004940 return NULL;
4941 }
4942
4943 return init;
4944}
4945
Giulio Camuffo459137b2014-10-11 23:56:24 +03004946
4947/** Destroys the compositor.
4948 *
4949 * This function cleans up the compositor state and destroys it.
4950 *
4951 * \param compositor The compositor to be destroyed.
4952 */
4953WL_EXPORT void
4954weston_compositor_destroy(struct weston_compositor *compositor)
4955{
4956 /* prevent further rendering while shutting down */
4957 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4958
4959 wl_signal_emit(&compositor->destroy_signal, compositor);
4960
4961 weston_compositor_xkb_destroy(compositor);
4962
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004963 if (compositor->backend)
4964 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004965
4966 weston_plugin_api_destroy_list(compositor);
4967
Giulio Camuffo459137b2014-10-11 23:56:24 +03004968 free(compositor);
4969}
4970
4971/** Instruct the compositor to exit.
4972 *
4973 * This functions does not directly destroy the compositor object, it merely
4974 * command it to start the tear down process. It is not guaranteed that the
4975 * tear down will happen immediately.
4976 *
4977 * \param compositor The compositor to tear down.
4978 */
4979WL_EXPORT void
4980weston_compositor_exit(struct weston_compositor *compositor)
4981{
4982 compositor->exit(compositor);
4983}
4984
4985/** Return the user data stored in the compositor.
4986 *
4987 * This function returns the user data pointer set with user_data parameter
4988 * to the \ref weston_compositor_create function.
4989 */
4990WL_EXPORT void *
4991weston_compositor_get_user_data(struct weston_compositor *compositor)
4992{
4993 return compositor->user_data;
4994}
Pekka Paalanendd186732016-06-03 14:49:54 +03004995
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004996static const char * const backend_map[] = {
4997 [WESTON_BACKEND_DRM] = "drm-backend.so",
4998 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
4999 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5000 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5001 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5002 [WESTON_BACKEND_X11] = "x11-backend.so",
5003};
5004
Pekka Paalanendd186732016-06-03 14:49:54 +03005005/** Load a backend into a weston_compositor
5006 *
5007 * A backend must be loaded to make a weston_compositor work. A backend
5008 * provides input and output capabilities, and determines the renderer to use.
5009 *
5010 * \param compositor A compositor that has not had a backend loaded yet.
5011 * \param backend Name of the backend file.
5012 * \param config_base A pointer to a backend-specific configuration
5013 * structure's 'base' member.
5014 *
5015 * \return 0 on success, or -1 on error.
5016 */
5017WL_EXPORT int
5018weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005019 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005020 struct weston_backend_config *config_base)
5021{
5022 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005023 struct weston_backend_config *config_base);
5024
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005025 if (backend < 0 || backend >= ARRAY_LENGTH(backend_map))
5026 return -1;
5027
5028 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005029 if (!backend_init)
5030 return -1;
5031
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005032 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005033}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005034
5035WL_EXPORT int
5036weston_compositor_load_xwayland(struct weston_compositor *compositor)
5037{
5038 int (*module_init)(struct weston_compositor *ec,
5039 int *argc, char *argv[]);
5040 int argc = 0;
5041
5042 module_init = weston_load_module("xwayland.so", "module_init");
5043 if (!module_init)
5044 return -1;
5045 if (module_init(compositor, &argc, NULL) < 0)
5046 return -1;
5047 return 0;
5048}