blob: 5abef52e917c4f779c743775decfe4abe35e91da [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
Derek Foreman280e7dd2014-10-03 13:13:42 -05001547WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548weston_view_is_mapped(struct weston_view *view)
1549{
1550 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001551 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001553 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001554}
1555
Derek Foreman280e7dd2014-10-03 13:13:42 -05001556WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001557weston_surface_is_mapped(struct weston_surface *surface)
1558{
1559 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001560 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001561 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001562 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001563}
1564
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001565static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001566surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001567{
1568 struct weston_view *view;
1569
1570 if (surface->width == width && surface->height == height)
1571 return;
1572
1573 surface->width = width;
1574 surface->height = height;
1575
1576 wl_list_for_each(view, &surface->views, surface_link)
1577 weston_view_geometry_dirty(view);
1578}
1579
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001580WL_EXPORT void
1581weston_surface_set_size(struct weston_surface *surface,
1582 int32_t width, int32_t height)
1583{
1584 assert(!surface->resource);
1585 surface_set_size(surface, width, height);
1586}
1587
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001588static int
1589fixed_round_up_to_int(wl_fixed_t f)
1590{
1591 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1592}
1593
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001594static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001595convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1596 int32_t width, int32_t height,
1597 uint32_t transform,
1598 int32_t scale)
1599{
1600 assert(scale > 0);
1601
1602 switch (transform) {
1603 case WL_OUTPUT_TRANSFORM_NORMAL:
1604 case WL_OUTPUT_TRANSFORM_180:
1605 case WL_OUTPUT_TRANSFORM_FLIPPED:
1606 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1607 *width_out = width / scale;
1608 *height_out = height / scale;
1609 break;
1610 case WL_OUTPUT_TRANSFORM_90:
1611 case WL_OUTPUT_TRANSFORM_270:
1612 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1613 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1614 *width_out = height / scale;
1615 *height_out = width / scale;
1616 break;
1617 default:
1618 assert(0 && "invalid transform");
1619 }
1620}
1621
1622static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001623weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001624{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001625 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001626
1627 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001628 surface->width_from_buffer = 0;
1629 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001630 return;
1631 }
1632
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001633 convert_size_by_transform_scale(&surface->width_from_buffer,
1634 &surface->height_from_buffer,
1635 surface->buffer_ref.buffer->width,
1636 surface->buffer_ref.buffer->height,
1637 vp->buffer.transform,
1638 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001639}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001640
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001641static void
1642weston_surface_update_size(struct weston_surface *surface)
1643{
1644 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1645 int32_t width, height;
1646
1647 width = surface->width_from_buffer;
1648 height = surface->height_from_buffer;
1649
1650 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001651 surface_set_size(surface,
1652 vp->surface.width, vp->surface.height);
1653 return;
1654 }
1655
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001656 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001657 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1658 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1659
1660 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001661 return;
1662 }
1663
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001664 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001665}
1666
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001667WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001668weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001669{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001670 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001671
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001672 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001673
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001674 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001675}
1676
Jason Ekstranda7af7042013-10-12 22:38:11 -05001677WL_EXPORT struct weston_view *
1678weston_compositor_pick_view(struct weston_compositor *compositor,
1679 wl_fixed_t x, wl_fixed_t y,
1680 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001681{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001683 wl_fixed_t view_x, view_y;
1684 int view_ix, view_iy;
1685 int ix = wl_fixed_to_int(x);
1686 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001687
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001689 if (!pixman_region32_contains_point(
1690 &view->transform.boundingbox, ix, iy, NULL))
1691 continue;
1692
1693 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1694 view_ix = wl_fixed_to_int(view_x);
1695 view_iy = wl_fixed_to_int(view_y);
1696
1697 if (!pixman_region32_contains_point(&view->surface->input,
1698 view_ix, view_iy, NULL))
1699 continue;
1700
Pekka Paalanen380adf52015-02-16 14:39:11 +02001701 if (view->geometry.scissor_enabled &&
1702 !pixman_region32_contains_point(&view->geometry.scissor,
1703 view_ix, view_iy, NULL))
1704 continue;
1705
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001706 *vx = view_x;
1707 *vy = view_y;
1708 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001709 }
1710
Derek Foremanf9318d12015-05-11 15:40:11 -05001711 *vx = wl_fixed_from_int(-1000000);
1712 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001713 return NULL;
1714}
1715
1716static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001717weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001718{
Daniel Stone37816df2012-05-16 18:45:18 +01001719 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001720
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001721 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001722 return;
1723
Daniel Stone37816df2012-05-16 18:45:18 +01001724 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001725 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001726}
1727
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001728WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001729weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001730{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001731 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001732
Jason Ekstranda7af7042013-10-12 22:38:11 -05001733 if (!weston_view_is_mapped(view))
1734 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001735
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736 weston_view_damage_below(view);
1737 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001738 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001739 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001740 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 wl_list_remove(&view->link);
1742 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743 view->output_mask = 0;
1744 weston_surface_assign_output(view->surface);
1745
1746 if (weston_surface_is_mapped(view->surface))
1747 return;
1748
1749 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001750 struct weston_touch *touch = weston_seat_get_touch(seat);
1751 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1752 struct weston_keyboard *keyboard =
1753 weston_seat_get_keyboard(seat);
1754
1755 if (keyboard && keyboard->focus == view->surface)
1756 weston_keyboard_set_focus(keyboard, NULL);
1757 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001758 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001759 if (touch && touch->focus == view)
1760 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001761 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001763
Jason Ekstranda7af7042013-10-12 22:38:11 -05001764WL_EXPORT void
1765weston_surface_unmap(struct weston_surface *surface)
1766{
1767 struct weston_view *view;
1768
Armin Krezovićf8486c32016-06-30 06:04:28 +02001769 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770 wl_list_for_each(view, &surface->views, surface_link)
1771 weston_view_unmap(view);
1772 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001773}
1774
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001775static void
1776weston_surface_reset_pending_buffer(struct weston_surface *surface)
1777{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001778 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001779 surface->pending.sx = 0;
1780 surface->pending.sy = 0;
1781 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001782 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001783}
1784
Jason Ekstranda7af7042013-10-12 22:38:11 -05001785WL_EXPORT void
1786weston_view_destroy(struct weston_view *view)
1787{
1788 wl_signal_emit(&view->destroy_signal, view);
1789
1790 assert(wl_list_empty(&view->geometry.child_list));
1791
1792 if (weston_view_is_mapped(view)) {
1793 weston_view_unmap(view);
1794 weston_compositor_build_view_list(view->surface->compositor);
1795 }
1796
1797 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001798 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799
1800 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001801 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001803 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804
1805 weston_view_set_transform_parent(view, NULL);
1806
Jason Ekstranda7af7042013-10-12 22:38:11 -05001807 wl_list_remove(&view->surface_link);
1808
1809 free(view);
1810}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001811
1812WL_EXPORT void
1813weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001814{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001815 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001817
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001818 if (--surface->ref_count > 0)
1819 return;
1820
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001821 assert(surface->resource == NULL);
1822
Pekka Paalanenca790762015-04-17 14:23:38 +03001823 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001824
Pekka Paalanene67858b2013-04-25 13:57:42 +03001825 assert(wl_list_empty(&surface->subsurface_list_pending));
1826 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001827
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1829 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001830
Jason Ekstrand7b982072014-05-20 14:33:03 -05001831 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001832
Pekka Paalanende685b82012-12-04 15:58:12 +02001833 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001834
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001835 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001836 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001837 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001838
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001839 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001840 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001841
Pekka Paalanen133e4392014-09-23 22:08:46 -04001842 weston_presentation_feedback_discard_list(&surface->feedback_list);
1843
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001844 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001845}
1846
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001847static void
1848destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001849{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001850 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001851
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001852 assert(surface);
1853
Giulio Camuffo0d379742013-11-15 22:06:15 +01001854 /* Set the resource to NULL, since we don't want to leave a
1855 * dangling pointer if the surface was refcounted and survives
1856 * the weston_surface_destroy() call. */
1857 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001858
1859 if (surface->viewport_resource)
1860 wl_resource_set_user_data(surface->viewport_resource, NULL);
1861
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001862 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001863}
1864
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001865static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001866weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1867{
1868 struct weston_buffer *buffer =
1869 container_of(listener, struct weston_buffer, destroy_listener);
1870
1871 wl_signal_emit(&buffer->destroy_signal, buffer);
1872 free(buffer);
1873}
1874
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001875WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001876weston_buffer_from_resource(struct wl_resource *resource)
1877{
1878 struct weston_buffer *buffer;
1879 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001880
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001881 listener = wl_resource_get_destroy_listener(resource,
1882 weston_buffer_destroy_handler);
1883
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001884 if (listener)
1885 return container_of(listener, struct weston_buffer,
1886 destroy_listener);
1887
1888 buffer = zalloc(sizeof *buffer);
1889 if (buffer == NULL)
1890 return NULL;
1891
1892 buffer->resource = resource;
1893 wl_signal_init(&buffer->destroy_signal);
1894 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001895 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001896 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001897
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001898 return buffer;
1899}
1900
1901static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001902weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1903 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001904{
Pekka Paalanende685b82012-12-04 15:58:12 +02001905 struct weston_buffer_reference *ref =
1906 container_of(listener, struct weston_buffer_reference,
1907 destroy_listener);
1908
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001909 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001910 ref->buffer = NULL;
1911}
1912
1913WL_EXPORT void
1914weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001915 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001916{
1917 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001918 ref->buffer->busy_count--;
1919 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001920 assert(wl_resource_get_client(ref->buffer->resource));
1921 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001922 WL_BUFFER_RELEASE);
1923 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001924 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001925 }
1926
Pekka Paalanende685b82012-12-04 15:58:12 +02001927 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001928 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001929 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001930 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001931 }
1932
Pekka Paalanende685b82012-12-04 15:58:12 +02001933 ref->buffer = buffer;
1934 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1935}
1936
1937static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001938weston_surface_attach(struct weston_surface *surface,
1939 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001940{
1941 weston_buffer_reference(&surface->buffer_ref, buffer);
1942
Pekka Paalanena6421c42012-12-04 15:58:10 +02001943 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001944 if (weston_surface_is_mapped(surface))
1945 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001946 }
1947
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001948 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001949
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001950 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001951 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001952}
1953
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001954WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001955weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001956{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001957 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001958
1959 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001960 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001961}
1962
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001963WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001964weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001965{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001966 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001967
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001968 pixman_region32_union(&compositor->primary_plane.damage,
1969 &compositor->primary_plane.damage,
1970 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001971 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001972}
1973
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001974static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001975surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001976{
Pekka Paalanende685b82012-12-04 15:58:12 +02001977 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001978 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001979 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001980
Pekka Paalanenb5026542014-11-12 15:09:24 +02001981 if (weston_timeline_enabled_ &&
1982 pixman_region32_not_empty(&surface->damage))
1983 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1984 TLP_OUTPUT(surface->output), TLP_END);
1985
Jason Ekstrandef540082014-06-26 10:37:36 -07001986 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001987}
1988
1989static void
1990view_accumulate_damage(struct weston_view *view,
1991 pixman_region32_t *opaque)
1992{
1993 pixman_region32_t damage;
1994
1995 pixman_region32_init(&damage);
1996 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001997 pixman_box32_t *extents;
1998
Jason Ekstranda7af7042013-10-12 22:38:11 -05001999 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002000 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002001 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002002 pixman_region32_copy(&damage, &view->surface->damage);
2003 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002004 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002005 }
2006
Pekka Paalanen380adf52015-02-16 14:39:11 +02002007 pixman_region32_intersect(&damage, &damage,
2008 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002009 pixman_region32_subtract(&damage, &damage, opaque);
2010 pixman_region32_union(&view->plane->damage,
2011 &view->plane->damage, &damage);
2012 pixman_region32_fini(&damage);
2013 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002014 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002015}
2016
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002017static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002018compositor_accumulate_damage(struct weston_compositor *ec)
2019{
2020 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002021 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002022 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002023
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002024 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002025
2026 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002027 pixman_region32_copy(&plane->clip, &clip);
2028
2029 pixman_region32_init(&opaque);
2030
Jason Ekstranda7af7042013-10-12 22:38:11 -05002031 wl_list_for_each(ev, &ec->view_list, link) {
2032 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002033 continue;
2034
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002036 }
2037
2038 pixman_region32_union(&clip, &clip, &opaque);
2039 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002040 }
2041
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002042 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002043
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002045 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046
2047 wl_list_for_each(ev, &ec->view_list, link) {
2048 if (ev->surface->touched)
2049 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002050 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002051
2052 surface_flush_damage(ev->surface);
2053
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002054 /* Both the renderer and the backend have seen the buffer
2055 * by now. If renderer needs the buffer, it has its own
2056 * reference set. If the backend wants to keep the buffer
2057 * around for migrating the surface into a non-primary plane
2058 * later, keep_buffer is true. Otherwise, drop the core
2059 * reference now, and allow early buffer release. This enables
2060 * clients to use single-buffering.
2061 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002062 if (!ev->surface->keep_buffer)
2063 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002064 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002065}
2066
2067static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002068surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002069{
2070 struct weston_subsurface *sub;
2071
Pekka Paalanene67858b2013-04-25 13:57:42 +03002072 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002073 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002074 continue;
2075
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2077 wl_list_init(&sub->surface->views);
2078
2079 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002080 }
2081}
2082
2083static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002084surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002085{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002086 struct weston_subsurface *sub;
2087 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002088
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2090 if (sub->surface == surface)
2091 continue;
2092
George Kiagiadakised04d382014-06-13 18:10:26 +02002093 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2094 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002095 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002096 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002097
2098 surface_free_unused_subsurface_views(sub->surface);
2099 }
2100}
2101
2102static void
2103view_list_add_subsurface_view(struct weston_compositor *compositor,
2104 struct weston_subsurface *sub,
2105 struct weston_view *parent)
2106{
2107 struct weston_subsurface *child;
2108 struct weston_view *view = NULL, *iv;
2109
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002110 if (!weston_surface_is_mapped(sub->surface))
2111 return;
2112
Jason Ekstranda7af7042013-10-12 22:38:11 -05002113 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2114 if (iv->geometry.parent == parent) {
2115 view = iv;
2116 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002117 }
2118 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002119
2120 if (view) {
2121 /* Put it back in the surface's list of views */
2122 wl_list_remove(&view->surface_link);
2123 wl_list_insert(&sub->surface->views, &view->surface_link);
2124 } else {
2125 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002126 weston_view_set_position(view,
2127 sub->position.x,
2128 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002129 weston_view_set_transform_parent(view, parent);
2130 }
2131
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002132 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002133 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002134 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002135
Pekka Paalanenb188e912013-11-19 14:03:35 +02002136 if (wl_list_empty(&sub->surface->subsurface_list)) {
2137 wl_list_insert(compositor->view_list.prev, &view->link);
2138 return;
2139 }
2140
2141 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2142 if (child->surface == sub->surface)
2143 wl_list_insert(compositor->view_list.prev, &view->link);
2144 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002146 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147}
2148
2149static void
2150view_list_add(struct weston_compositor *compositor,
2151 struct weston_view *view)
2152{
2153 struct weston_subsurface *sub;
2154
2155 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002156
Pekka Paalanenb188e912013-11-19 14:03:35 +02002157 if (wl_list_empty(&view->surface->subsurface_list)) {
2158 wl_list_insert(compositor->view_list.prev, &view->link);
2159 return;
2160 }
2161
2162 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2163 if (sub->surface == view->surface)
2164 wl_list_insert(compositor->view_list.prev, &view->link);
2165 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002166 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002167 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002168}
2169
2170static void
2171weston_compositor_build_view_list(struct weston_compositor *compositor)
2172{
2173 struct weston_view *view;
2174 struct weston_layer *layer;
2175
2176 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002177 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178 surface_stash_subsurface_views(view->surface);
2179
2180 wl_list_init(&compositor->view_list);
2181 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002182 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002183 view_list_add(compositor, view);
2184 }
2185 }
2186
2187 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002188 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002189 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002190}
2191
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002192static void
2193weston_output_take_feedback_list(struct weston_output *output,
2194 struct weston_surface *surface)
2195{
2196 struct weston_view *view;
2197 struct weston_presentation_feedback *feedback;
2198 uint32_t flags = 0xffffffff;
2199
2200 if (wl_list_empty(&surface->feedback_list))
2201 return;
2202
2203 /* All views must have the flag for the flag to survive. */
2204 wl_list_for_each(view, &surface->views, surface_link) {
2205 /* ignore views that are not on this output at all */
2206 if (view->output_mask & (1u << output->id))
2207 flags &= view->psf_flags;
2208 }
2209
2210 wl_list_for_each(feedback, &surface->feedback_list, link)
2211 feedback->psf_flags = flags;
2212
2213 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2214 wl_list_init(&surface->feedback_list);
2215}
2216
David Herrmann1edf44c2013-10-22 17:11:26 +02002217static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002218weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002219{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002220 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002221 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002222 struct weston_animation *animation, *next;
2223 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002224 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002225 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002226 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002227
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002228 if (output->destroying)
2229 return 0;
2230
Pekka Paalanenb5026542014-11-12 15:09:24 +02002231 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2232
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002233 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002234 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002235
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002236 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002237 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002238 } else {
2239 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002240 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002241 ev->psf_flags = 0;
2242 }
2243 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002244
Pekka Paalanene67858b2013-04-25 13:57:42 +03002245 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002246 wl_list_for_each(ev, &ec->view_list, link) {
2247 /* Note: This operation is safe to do multiple times on the
2248 * same surface.
2249 */
2250 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002251 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002252 &ev->surface->frame_callback_list);
2253 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002254
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002255 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002256 }
2257 }
2258
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002259 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002260
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002261 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002262 pixman_region32_intersect(&output_damage,
2263 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002264 pixman_region32_subtract(&output_damage,
2265 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002266
Scott Moreauccbf29d2012-02-22 14:21:41 -07002267 if (output->dirty)
2268 weston_output_update_matrix(output);
2269
David Herrmann1edf44c2013-10-22 17:11:26 +02002270 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002271
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002272 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002273
Kristian Høgsbergef044142011-06-21 15:02:12 -04002274 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002275
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002276 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002277
Jonas Ådahldb773762012-06-13 00:01:21 +02002278 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002279 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002280 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002281 }
2282
Scott Moreaud64cf212012-06-08 19:40:54 -06002283 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002284 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002285 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002286 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002287
Pekka Paalanenb5026542014-11-12 15:09:24 +02002288 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2289
David Herrmann1edf44c2013-10-22 17:11:26 +02002290 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002291}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002292
Pekka Paalanen82919792014-05-21 13:51:49 +03002293static void
2294weston_output_schedule_repaint_reset(struct weston_output *output)
2295{
Pekka Paalanen82919792014-05-21 13:51:49 +03002296 output->repaint_scheduled = 0;
2297 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002298}
2299
Pekka Paalanen0513a952014-05-21 16:17:27 +03002300static int
2301output_repaint_timer_handler(void *data)
2302{
2303 struct weston_output *output = data;
2304 struct weston_compositor *compositor = output->compositor;
2305
2306 if (output->repaint_needed &&
2307 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2308 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2309 weston_output_repaint(output) == 0)
2310 return 0;
2311
2312 weston_output_schedule_repaint_reset(output);
2313
2314 return 0;
2315}
2316
Kristian Høgsbergef044142011-06-21 15:02:12 -04002317WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002318weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002319 const struct timespec *stamp,
2320 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002321{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002322 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002323 int32_t refresh_nsec;
2324 struct timespec now;
2325 struct timespec gone;
2326 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002327
Pekka Paalanenb5026542014-11-12 15:09:24 +02002328 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2329 TLP_VBLANK(stamp), TLP_END);
2330
Pekka Paalanend7894d02015-07-03 15:08:53 +03002331 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002332 weston_presentation_feedback_present_list(&output->feedback_list,
2333 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002334 output->msc,
2335 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002336
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002337 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002338
Pekka Paalanen0513a952014-05-21 16:17:27 +03002339 weston_compositor_read_presentation_clock(compositor, &now);
2340 timespec_sub(&gone, &now, stamp);
2341 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2342 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002343
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002344 if (msec < -1000 || msec > 1000) {
2345 static bool warned;
2346
2347 if (!warned)
2348 weston_log("Warning: computed repaint delay is "
2349 "insane: %d msec\n", msec);
2350 warned = true;
2351
2352 msec = 0;
2353 }
2354
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002355 /* Called from restart_repaint_loop and restart happens already after
2356 * the deadline given by repaint_msec? In that case we delay until
2357 * the deadline of the next frame, to give clients a more predictable
2358 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002359 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002360 msec += refresh_nsec / 1000000;
2361
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002362 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002363 output_repaint_timer_handler(output);
2364 else
2365 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002366}
2367
2368static void
2369idle_repaint(void *data)
2370{
2371 struct weston_output *output = data;
2372
Jonas Ådahle5a12252013-04-05 23:07:11 +02002373 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002374}
2375
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002376WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002377weston_layer_entry_insert(struct weston_layer_entry *list,
2378 struct weston_layer_entry *entry)
2379{
2380 wl_list_insert(&list->link, &entry->link);
2381 entry->layer = list->layer;
2382}
2383
2384WL_EXPORT void
2385weston_layer_entry_remove(struct weston_layer_entry *entry)
2386{
2387 wl_list_remove(&entry->link);
2388 wl_list_init(&entry->link);
2389 entry->layer = NULL;
2390}
2391
2392WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002393weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2394{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002395 wl_list_init(&layer->view_list.link);
2396 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002397 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002398 if (below != NULL)
2399 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002400}
2401
2402WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002403weston_layer_set_mask(struct weston_layer *layer,
2404 int x, int y, int width, int height)
2405{
2406 struct weston_view *view;
2407
2408 layer->mask.x1 = x;
2409 layer->mask.x2 = x + width;
2410 layer->mask.y1 = y;
2411 layer->mask.y2 = y + height;
2412
2413 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2414 weston_view_geometry_dirty(view);
2415 }
2416}
2417
2418WL_EXPORT void
2419weston_layer_set_mask_infinite(struct weston_layer *layer)
2420{
2421 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2422 UINT32_MAX, UINT32_MAX);
2423}
2424
2425WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002426weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002427{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002428 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002429 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002430
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002431 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2432 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002433 return;
2434
Pekka Paalanenb5026542014-11-12 15:09:24 +02002435 if (!output->repaint_needed)
2436 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2437
Kristian Høgsbergef044142011-06-21 15:02:12 -04002438 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002439 output->repaint_needed = 1;
2440 if (output->repaint_scheduled)
2441 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002442
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002443 wl_event_loop_add_idle(loop, idle_repaint, output);
2444 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002445 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002446}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002447
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002448WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002449weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2450{
2451 struct weston_output *output;
2452
2453 wl_list_for_each(output, &compositor->output_list, link)
2454 weston_output_schedule_repaint(output);
2455}
2456
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002457static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002458surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002459{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002460 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002461}
2462
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002463static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002464surface_attach(struct wl_client *client,
2465 struct wl_resource *resource,
2466 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2467{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002468 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002469 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002470
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002471 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002472 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002473 if (buffer == NULL) {
2474 wl_client_post_no_memory(client);
2475 return;
2476 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002477 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002478
Pekka Paalanende685b82012-12-04 15:58:12 +02002479 /* Attach, attach, without commit in between does not send
2480 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002481 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002482
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002483 surface->pending.sx = sx;
2484 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002485 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002486}
2487
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002488static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002489surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002490 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002491 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002492{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002493 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002494
Derek Foreman57e92ed2015-11-17 14:11:35 -06002495 if (width <= 0 || height <= 0)
2496 return;
2497
Derek Foreman152254b2015-11-26 14:17:48 -06002498 pixman_region32_union_rect(&surface->pending.damage_surface,
2499 &surface->pending.damage_surface,
2500 x, y, width, height);
2501}
2502
2503static void
2504surface_damage_buffer(struct wl_client *client,
2505 struct wl_resource *resource,
2506 int32_t x, int32_t y, int32_t width, int32_t height)
2507{
2508 struct weston_surface *surface = wl_resource_get_user_data(resource);
2509
2510 if (width <= 0 || height <= 0)
2511 return;
2512
2513 pixman_region32_union_rect(&surface->pending.damage_buffer,
2514 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002515 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002516}
2517
Kristian Høgsberg33418202011-08-16 23:01:28 -04002518static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002519destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002520{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002521 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002522
2523 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002524 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002525}
2526
2527static void
2528surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002529 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002530{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002531 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002532 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002533
2534 cb = malloc(sizeof *cb);
2535 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002536 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002537 return;
2538 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002539
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002540 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2541 callback);
2542 if (cb->resource == NULL) {
2543 free(cb);
2544 wl_resource_post_no_memory(resource);
2545 return;
2546 }
2547
Jason Ekstranda85118c2013-06-27 20:17:02 -05002548 wl_resource_set_implementation(cb->resource, NULL, cb,
2549 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002550
Pekka Paalanenbc106382012-10-10 12:49:31 +03002551 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002552}
2553
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002554static void
2555surface_set_opaque_region(struct wl_client *client,
2556 struct wl_resource *resource,
2557 struct wl_resource *region_resource)
2558{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002559 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002560 struct weston_region *region;
2561
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002562 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002563 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002564 pixman_region32_copy(&surface->pending.opaque,
2565 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002566 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002567 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002568 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002569}
2570
2571static void
2572surface_set_input_region(struct wl_client *client,
2573 struct wl_resource *resource,
2574 struct wl_resource *region_resource)
2575{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002576 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002577 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002578
2579 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002580 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002581 pixman_region32_copy(&surface->pending.input,
2582 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002583 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002584 pixman_region32_fini(&surface->pending.input);
2585 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002586 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002587}
2588
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002589static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002590weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002591{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002592 struct weston_subsurface *sub;
2593
2594 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2595 parent_link_pending) {
2596 wl_list_remove(&sub->parent_link);
2597 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2598 }
2599}
2600
2601static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002602weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002603 struct weston_matrix *matrix)
2604{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002605 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002606 double src_width, src_height, dest_width, dest_height;
2607
2608 weston_matrix_init(matrix);
2609
2610 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2611 src_width = surface->width_from_buffer;
2612 src_height = surface->height_from_buffer;
2613 } else {
2614 src_width = wl_fixed_to_double(vp->buffer.src_width);
2615 src_height = wl_fixed_to_double(vp->buffer.src_height);
2616 }
2617
2618 if (vp->surface.width == -1) {
2619 dest_width = src_width;
2620 dest_height = src_height;
2621 } else {
2622 dest_width = vp->surface.width;
2623 dest_height = vp->surface.height;
2624 }
2625
2626 if (src_width != dest_width || src_height != dest_height)
2627 weston_matrix_scale(matrix,
2628 src_width / dest_width,
2629 src_height / dest_height, 1);
2630
2631 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2632 weston_matrix_translate(matrix,
2633 wl_fixed_to_double(vp->buffer.src_x),
2634 wl_fixed_to_double(vp->buffer.src_y),
2635 0);
2636
2637 switch (vp->buffer.transform) {
2638 case WL_OUTPUT_TRANSFORM_FLIPPED:
2639 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2640 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2641 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2642 weston_matrix_scale(matrix, -1, 1, 1);
2643 weston_matrix_translate(matrix,
2644 surface->width_from_buffer, 0, 0);
2645 break;
2646 }
2647
2648 switch (vp->buffer.transform) {
2649 default:
2650 case WL_OUTPUT_TRANSFORM_NORMAL:
2651 case WL_OUTPUT_TRANSFORM_FLIPPED:
2652 break;
2653 case WL_OUTPUT_TRANSFORM_90:
2654 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2655 weston_matrix_rotate_xy(matrix, 0, 1);
2656 weston_matrix_translate(matrix,
2657 surface->height_from_buffer, 0, 0);
2658 break;
2659 case WL_OUTPUT_TRANSFORM_180:
2660 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2661 weston_matrix_rotate_xy(matrix, -1, 0);
2662 weston_matrix_translate(matrix,
2663 surface->width_from_buffer,
2664 surface->height_from_buffer, 0);
2665 break;
2666 case WL_OUTPUT_TRANSFORM_270:
2667 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2668 weston_matrix_rotate_xy(matrix, 0, -1);
2669 weston_matrix_translate(matrix,
2670 0, surface->width_from_buffer, 0);
2671 break;
2672 }
2673
2674 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2675}
2676
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002677/**
2678 * Compute a + b > c while being safe to overflows.
2679 */
2680static bool
2681fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2682{
2683 return (int64_t)a + (int64_t)b > (int64_t)c;
2684}
2685
2686static bool
2687weston_surface_is_pending_viewport_source_valid(
2688 const struct weston_surface *surface)
2689{
2690 const struct weston_surface_state *pend = &surface->pending;
2691 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2692 int width_from_buffer = 0;
2693 int height_from_buffer = 0;
2694 wl_fixed_t w;
2695 wl_fixed_t h;
2696
2697 /* If viewport source rect is not set, it is always ok. */
2698 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2699 return true;
2700
2701 if (pend->newly_attached) {
2702 if (pend->buffer) {
2703 convert_size_by_transform_scale(&width_from_buffer,
2704 &height_from_buffer,
2705 pend->buffer->width,
2706 pend->buffer->height,
2707 vp->buffer.transform,
2708 vp->buffer.scale);
2709 } else {
2710 /* No buffer: viewport is irrelevant. */
2711 return true;
2712 }
2713 } else {
2714 width_from_buffer = surface->width_from_buffer;
2715 height_from_buffer = surface->height_from_buffer;
2716 }
2717
2718 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2719 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2720
2721 /* No buffer: viewport is irrelevant. */
2722 if (width_from_buffer == 0 || height_from_buffer == 0)
2723 return true;
2724
2725 /* overflow checks for wl_fixed_from_int() */
2726 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2727 return false;
2728 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2729 return false;
2730
2731 w = wl_fixed_from_int(width_from_buffer);
2732 h = wl_fixed_from_int(height_from_buffer);
2733
2734 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2735 return false;
2736 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2737 return false;
2738
2739 return true;
2740}
2741
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002742static bool
2743fixed_is_integer(wl_fixed_t v)
2744{
2745 return (v & 0xff) == 0;
2746}
2747
2748static bool
2749weston_surface_is_pending_viewport_dst_size_int(
2750 const struct weston_surface *surface)
2751{
2752 const struct weston_buffer_viewport *vp =
2753 &surface->pending.buffer_viewport;
2754
2755 if (vp->surface.width != -1) {
2756 assert(vp->surface.width > 0 && vp->surface.height > 0);
2757 return true;
2758 }
2759
2760 return fixed_is_integer(vp->buffer.src_width) &&
2761 fixed_is_integer(vp->buffer.src_height);
2762}
2763
Derek Foreman152254b2015-11-26 14:17:48 -06002764/* Translate pending damage in buffer co-ordinates to surface
2765 * co-ordinates and union it with a pixman_region32_t.
2766 * This should only be called after the buffer is attached.
2767 */
2768static void
2769apply_damage_buffer(pixman_region32_t *dest,
2770 struct weston_surface *surface,
2771 struct weston_surface_state *state)
2772{
2773 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2774
2775 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2776 * translated into surface co-ordinates and unioned with
2777 * any other surface damage.
2778 * None of this makes sense if there is no buffer though.
2779 */
2780 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2781 pixman_region32_t buffer_damage;
2782
2783 pixman_region32_intersect_rect(&state->damage_buffer,
2784 &state->damage_buffer,
2785 0, 0, buffer->width,
2786 buffer->height);
2787 pixman_region32_init(&buffer_damage);
2788 weston_matrix_transform_region(&buffer_damage,
2789 &surface->buffer_to_surface_matrix,
2790 &state->damage_buffer);
2791 pixman_region32_union(dest, dest, &buffer_damage);
2792 pixman_region32_fini(&buffer_damage);
2793 }
2794 /* We should clear this on commit even if there was no buffer */
2795 pixman_region32_clear(&state->damage_buffer);
2796}
2797
Jason Ekstrand1e059042014-10-16 10:55:19 -05002798static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002799weston_surface_commit_state(struct weston_surface *surface,
2800 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002801{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002802 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002803 pixman_region32_t opaque;
2804
Alexander Larsson4ea95522013-05-22 14:41:37 +02002805 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002806 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002807 /* wp_viewport.set_source */
2808 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002809 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002810
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002811 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002812 if (state->newly_attached)
2813 weston_surface_attach(surface, state->buffer);
2814 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002815
Jason Ekstrand1e059042014-10-16 10:55:19 -05002816 weston_surface_build_buffer_matrix(surface,
2817 &surface->surface_to_buffer_matrix);
2818 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2819 &surface->surface_to_buffer_matrix);
2820
Jason Ekstrand7b982072014-05-20 14:33:03 -05002821 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002822 weston_surface_update_size(surface);
2823 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002824 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002825 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002826
Jason Ekstrand7b982072014-05-20 14:33:03 -05002827 state->sx = 0;
2828 state->sy = 0;
2829 state->newly_attached = 0;
2830 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002831
Derek Foreman152254b2015-11-26 14:17:48 -06002832 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002833 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002834 (pixman_region32_not_empty(&state->damage_surface) ||
2835 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002836 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002837
Pekka Paalanen8e159182012-10-10 12:49:25 +03002838 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002839 &state->damage_surface);
2840
2841 apply_damage_buffer(&surface->damage, surface, state);
2842
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002843 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002844 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002845 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002846
2847 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002848 pixman_region32_init(&opaque);
2849 pixman_region32_intersect_rect(&opaque, &state->opaque,
2850 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002851
2852 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2853 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002854 wl_list_for_each(view, &surface->views, surface_link)
2855 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002856 }
2857
2858 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002859
2860 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002861 pixman_region32_intersect_rect(&surface->input, &state->input,
2862 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002863
Pekka Paalanenbc106382012-10-10 12:49:31 +03002864 /* wl_surface.frame */
2865 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002866 &state->frame_callback_list);
2867 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002868
2869 /* XXX:
2870 * What should happen with a feedback request, if there
2871 * is no wl_buffer attached for this commit?
2872 */
2873
2874 /* presentation.feedback */
2875 wl_list_insert_list(&surface->feedback_list,
2876 &state->feedback_list);
2877 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002878}
2879
2880static void
2881weston_surface_commit(struct weston_surface *surface)
2882{
2883 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002884
Pekka Paalanene67858b2013-04-25 13:57:42 +03002885 weston_surface_commit_subsurface_order(surface);
2886
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002887 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002888}
2889
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002890static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002891weston_subsurface_commit(struct weston_subsurface *sub);
2892
2893static void
2894weston_subsurface_parent_commit(struct weston_subsurface *sub,
2895 int parent_is_synchronized);
2896
2897static void
2898surface_commit(struct wl_client *client, struct wl_resource *resource)
2899{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002900 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002901 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2902
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002903 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2904 assert(surface->viewport_resource);
2905
2906 wl_resource_post_error(surface->viewport_resource,
2907 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2908 "wl_surface@%d has viewport source outside buffer",
2909 wl_resource_get_id(resource));
2910 return;
2911 }
2912
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002913 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
2914 assert(surface->viewport_resource);
2915
2916 wl_resource_post_error(surface->viewport_resource,
2917 WP_VIEWPORT_ERROR_BAD_SIZE,
2918 "wl_surface@%d viewport dst size not integer",
2919 wl_resource_get_id(resource));
2920 return;
2921 }
2922
Pekka Paalanene67858b2013-04-25 13:57:42 +03002923 if (sub) {
2924 weston_subsurface_commit(sub);
2925 return;
2926 }
2927
2928 weston_surface_commit(surface);
2929
2930 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2931 if (sub->surface != surface)
2932 weston_subsurface_parent_commit(sub, 0);
2933 }
2934}
2935
2936static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002937surface_set_buffer_transform(struct wl_client *client,
2938 struct wl_resource *resource, int transform)
2939{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002940 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002941
Jonny Lamba55f1392014-05-30 12:07:15 +02002942 /* if wl_output.transform grows more members this will need to be updated. */
2943 if (transform < 0 ||
2944 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2945 wl_resource_post_error(resource,
2946 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2947 "buffer transform must be a valid transform "
2948 "('%d' specified)", transform);
2949 return;
2950 }
2951
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002952 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002953 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002954}
2955
Alexander Larsson4ea95522013-05-22 14:41:37 +02002956static void
2957surface_set_buffer_scale(struct wl_client *client,
2958 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002959 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002960{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002961 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002962
Jonny Lamba55f1392014-05-30 12:07:15 +02002963 if (scale < 1) {
2964 wl_resource_post_error(resource,
2965 WL_SURFACE_ERROR_INVALID_SCALE,
2966 "buffer scale must be at least one "
2967 "('%d' specified)", scale);
2968 return;
2969 }
2970
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002971 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002972 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002973}
2974
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002975static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002976 surface_destroy,
2977 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002978 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002979 surface_frame,
2980 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002981 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002982 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002983 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06002984 surface_set_buffer_scale,
2985 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002986};
2987
2988static void
2989compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002990 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002991{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002992 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002993 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002994
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002995 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002996 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002997 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002998 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002999 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003000
Jason Ekstranda85118c2013-06-27 20:17:02 -05003001 surface->resource =
3002 wl_resource_create(client, &wl_surface_interface,
3003 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003004 if (surface->resource == NULL) {
3005 weston_surface_destroy(surface);
3006 wl_resource_post_no_memory(resource);
3007 return;
3008 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003009 wl_resource_set_implementation(surface->resource, &surface_interface,
3010 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003011
3012 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003013}
3014
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003015static void
3016destroy_region(struct wl_resource *resource)
3017{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003018 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003019
3020 pixman_region32_fini(&region->region);
3021 free(region);
3022}
3023
3024static void
3025region_destroy(struct wl_client *client, struct wl_resource *resource)
3026{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003027 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003028}
3029
3030static void
3031region_add(struct wl_client *client, struct wl_resource *resource,
3032 int32_t x, int32_t y, int32_t width, int32_t height)
3033{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003034 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003035
3036 pixman_region32_union_rect(&region->region, &region->region,
3037 x, y, width, height);
3038}
3039
3040static void
3041region_subtract(struct wl_client *client, struct wl_resource *resource,
3042 int32_t x, int32_t y, int32_t width, int32_t height)
3043{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003044 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003045 pixman_region32_t rect;
3046
3047 pixman_region32_init_rect(&rect, x, y, width, height);
3048 pixman_region32_subtract(&region->region, &region->region, &rect);
3049 pixman_region32_fini(&rect);
3050}
3051
3052static const struct wl_region_interface region_interface = {
3053 region_destroy,
3054 region_add,
3055 region_subtract
3056};
3057
3058static void
3059compositor_create_region(struct wl_client *client,
3060 struct wl_resource *resource, uint32_t id)
3061{
3062 struct weston_region *region;
3063
3064 region = malloc(sizeof *region);
3065 if (region == NULL) {
3066 wl_resource_post_no_memory(resource);
3067 return;
3068 }
3069
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003070 pixman_region32_init(&region->region);
3071
Jason Ekstranda85118c2013-06-27 20:17:02 -05003072 region->resource =
3073 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003074 if (region->resource == NULL) {
3075 free(region);
3076 wl_resource_post_no_memory(resource);
3077 return;
3078 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003079 wl_resource_set_implementation(region->resource, &region_interface,
3080 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003081}
3082
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003083static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003084 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003085 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003086};
3087
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003088static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003089weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3090{
3091 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003092
Jason Ekstrand7b982072014-05-20 14:33:03 -05003093 weston_surface_commit_state(surface, &sub->cached);
3094 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003095
3096 weston_surface_commit_subsurface_order(surface);
3097
3098 weston_surface_schedule_repaint(surface);
3099
Jason Ekstrand7b982072014-05-20 14:33:03 -05003100 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003101}
3102
3103static void
3104weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3105{
3106 struct weston_surface *surface = sub->surface;
3107
3108 /*
3109 * If this commit would cause the surface to move by the
3110 * attach(dx, dy) parameters, the old damage region must be
3111 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003112 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003113 */
Derek Foreman152254b2015-11-26 14:17:48 -06003114 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003115 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003116 pixman_region32_union(&sub->cached.damage_surface,
3117 &sub->cached.damage_surface,
3118 &surface->pending.damage_surface);
3119 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003120
3121 if (surface->pending.newly_attached) {
3122 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003123 weston_surface_state_set_buffer(&sub->cached,
3124 surface->pending.buffer);
3125 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003126 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003127 weston_presentation_feedback_discard_list(
3128 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003129 }
3130 sub->cached.sx += surface->pending.sx;
3131 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003132
Derek Foreman152254b2015-11-26 14:17:48 -06003133 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3134
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003135 sub->cached.buffer_viewport.changed |=
3136 surface->pending.buffer_viewport.changed;
3137 sub->cached.buffer_viewport.buffer =
3138 surface->pending.buffer_viewport.buffer;
3139 sub->cached.buffer_viewport.surface =
3140 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003141
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003142 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003143
3144 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3145
3146 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3147
3148 wl_list_insert_list(&sub->cached.frame_callback_list,
3149 &surface->pending.frame_callback_list);
3150 wl_list_init(&surface->pending.frame_callback_list);
3151
Pekka Paalanen133e4392014-09-23 22:08:46 -04003152 wl_list_insert_list(&sub->cached.feedback_list,
3153 &surface->pending.feedback_list);
3154 wl_list_init(&surface->pending.feedback_list);
3155
Jason Ekstrand7b982072014-05-20 14:33:03 -05003156 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003157}
3158
Derek Foreman280e7dd2014-10-03 13:13:42 -05003159static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003160weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3161{
3162 while (sub) {
3163 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003164 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003165
3166 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003167 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003168
3169 sub = weston_surface_to_subsurface(sub->parent);
3170 }
3171
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003172 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003173}
3174
3175static void
3176weston_subsurface_commit(struct weston_subsurface *sub)
3177{
3178 struct weston_surface *surface = sub->surface;
3179 struct weston_subsurface *tmp;
3180
3181 /* Recursive check for effectively synchronized. */
3182 if (weston_subsurface_is_synchronized(sub)) {
3183 weston_subsurface_commit_to_cache(sub);
3184 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003185 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003186 /* flush accumulated state from cache */
3187 weston_subsurface_commit_to_cache(sub);
3188 weston_subsurface_commit_from_cache(sub);
3189 } else {
3190 weston_surface_commit(surface);
3191 }
3192
3193 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3194 if (tmp->surface != surface)
3195 weston_subsurface_parent_commit(tmp, 0);
3196 }
3197 }
3198}
3199
3200static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003201weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003202{
3203 struct weston_surface *surface = sub->surface;
3204 struct weston_subsurface *tmp;
3205
Pekka Paalanene67858b2013-04-25 13:57:42 +03003206 /* From now on, commit_from_cache the whole sub-tree, regardless of
3207 * the synchronized mode of each child. This sub-surface or some
3208 * of its ancestors were synchronized, so we are synchronized
3209 * all the way down.
3210 */
3211
Jason Ekstrand7b982072014-05-20 14:33:03 -05003212 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003213 weston_subsurface_commit_from_cache(sub);
3214
3215 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3216 if (tmp->surface != surface)
3217 weston_subsurface_parent_commit(tmp, 1);
3218 }
3219}
3220
3221static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003222weston_subsurface_parent_commit(struct weston_subsurface *sub,
3223 int parent_is_synchronized)
3224{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003225 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003226 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003227 wl_list_for_each(view, &sub->surface->views, surface_link)
3228 weston_view_set_position(view,
3229 sub->position.x,
3230 sub->position.y);
3231
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003232 sub->position.set = 0;
3233 }
3234
3235 if (parent_is_synchronized || sub->synchronized)
3236 weston_subsurface_synchronized_commit(sub);
3237}
3238
Pekka Paalanen8274d902014-08-06 19:36:51 +03003239static int
3240subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3241{
3242 return snprintf(buf, len, "sub-surface");
3243}
3244
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003245static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003246subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003247{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003248 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003249
Jason Ekstranda7af7042013-10-12 22:38:11 -05003250 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003251 weston_view_set_position(view,
3252 view->geometry.x + dx,
3253 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003254
3255 /* No need to check parent mappedness, because if parent is not
3256 * mapped, parent is not in a visible layer, so this sub-surface
3257 * will not be drawn either.
3258 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003259
Pekka Paalanene67858b2013-04-25 13:57:42 +03003260 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003261 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003262
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003263 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003264 * because that would call it also for the parent surface,
3265 * which might not be mapped yet. That would lead to
3266 * inconsistent state, where the window could never be
3267 * mapped.
3268 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003269 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003270 * weston_surface_is_mapped() return true, so that when the
3271 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003272 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003273 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003274 }
3275}
3276
3277static struct weston_subsurface *
3278weston_surface_to_subsurface(struct weston_surface *surface)
3279{
3280 if (surface->configure == subsurface_configure)
3281 return surface->configure_private;
3282
3283 return NULL;
3284}
3285
Pekka Paalanen01388e22013-04-25 13:57:44 +03003286WL_EXPORT struct weston_surface *
3287weston_surface_get_main_surface(struct weston_surface *surface)
3288{
3289 struct weston_subsurface *sub;
3290
3291 while (surface && (sub = weston_surface_to_subsurface(surface)))
3292 surface = sub->parent;
3293
3294 return surface;
3295}
3296
Pekka Paalanen50b67472014-10-01 15:02:41 +03003297WL_EXPORT int
3298weston_surface_set_role(struct weston_surface *surface,
3299 const char *role_name,
3300 struct wl_resource *error_resource,
3301 uint32_t error_code)
3302{
3303 assert(role_name);
3304
3305 if (surface->role_name == NULL ||
3306 surface->role_name == role_name ||
3307 strcmp(surface->role_name, role_name) == 0) {
3308 surface->role_name = role_name;
3309
3310 return 0;
3311 }
3312
3313 wl_resource_post_error(error_resource, error_code,
3314 "Cannot assign role %s to wl_surface@%d,"
3315 " already has role %s\n",
3316 role_name,
3317 wl_resource_get_id(surface->resource),
3318 surface->role_name);
3319 return -1;
3320}
3321
Pekka Paalanen8274d902014-08-06 19:36:51 +03003322WL_EXPORT void
3323weston_surface_set_label_func(struct weston_surface *surface,
3324 int (*desc)(struct weston_surface *,
3325 char *, size_t))
3326{
3327 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003328 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003329}
3330
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003331/** Get the size of surface contents
3332 *
3333 * \param surface The surface to query.
3334 * \param width Returns the width of raw contents.
3335 * \param height Returns the height of raw contents.
3336 *
3337 * Retrieves the raw surface content size in pixels for the given surface.
3338 * This is the whole content size in buffer pixels. If the surface
3339 * has no content or the renderer does not implement this feature,
3340 * zeroes are returned.
3341 *
3342 * This function is used to determine the buffer size needed for
3343 * a weston_surface_copy_content() call.
3344 */
3345WL_EXPORT void
3346weston_surface_get_content_size(struct weston_surface *surface,
3347 int *width, int *height)
3348{
3349 struct weston_renderer *rer = surface->compositor->renderer;
3350
3351 if (!rer->surface_get_content_size) {
3352 *width = 0;
3353 *height = 0;
3354 return;
3355 }
3356
3357 rer->surface_get_content_size(surface, width, height);
3358}
3359
3360/** Copy surface contents to system memory.
3361 *
3362 * \param surface The surface to copy from.
3363 * \param target Pointer to the target memory buffer.
3364 * \param size Size of the target buffer in bytes.
3365 * \param src_x X location on contents to copy from.
3366 * \param src_y Y location on contents to copy from.
3367 * \param width Width in pixels of the area to copy.
3368 * \param height Height in pixels of the area to copy.
3369 * \return 0 for success, -1 for failure.
3370 *
3371 * Surface contents are maintained by the renderer. They can be in a
3372 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3373 * else.
3374 *
3375 * Surface contents are copied into memory pointed to by target,
3376 * which has size bytes of space available. The target memory
3377 * may be larger than needed, but being smaller returns an error.
3378 * The extra bytes in target may or may not be written; their content is
3379 * unspecified. Size must be large enough to hold the image.
3380 *
3381 * The image in the target memory will be arranged in rows from
3382 * top to bottom, and pixels on a row from left to right. The pixel
3383 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3384 * width * 4.
3385 *
3386 * Parameters src_x and src_y define the upper-left corner in buffer
3387 * coordinates (pixels) to copy from. Parameters width and height
3388 * define the size of the area to copy in pixels.
3389 *
3390 * The rectangle defined by src_x, src_y, width, height must fit in
3391 * the surface contents. Otherwise an error is returned.
3392 *
3393 * Use surface_get_data_size to determine the content size; the
3394 * needed target buffer size and rectangle limits.
3395 *
3396 * CURRENT IMPLEMENTATION RESTRICTIONS:
3397 * - the machine must be little-endian due to Pixman formats.
3398 *
3399 * NOTE: Pixman formats are premultiplied.
3400 */
3401WL_EXPORT int
3402weston_surface_copy_content(struct weston_surface *surface,
3403 void *target, size_t size,
3404 int src_x, int src_y,
3405 int width, int height)
3406{
3407 struct weston_renderer *rer = surface->compositor->renderer;
3408 int cw, ch;
3409 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3410
3411 if (!rer->surface_copy_content)
3412 return -1;
3413
3414 weston_surface_get_content_size(surface, &cw, &ch);
3415
3416 if (src_x < 0 || src_y < 0)
3417 return -1;
3418
3419 if (width <= 0 || height <= 0)
3420 return -1;
3421
3422 if (src_x + width > cw || src_y + height > ch)
3423 return -1;
3424
3425 if (width * bytespp * height > size)
3426 return -1;
3427
3428 return rer->surface_copy_content(surface, target, size,
3429 src_x, src_y, width, height);
3430}
3431
Pekka Paalanene67858b2013-04-25 13:57:42 +03003432static void
3433subsurface_set_position(struct wl_client *client,
3434 struct wl_resource *resource, int32_t x, int32_t y)
3435{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003436 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003437
3438 if (!sub)
3439 return;
3440
3441 sub->position.x = x;
3442 sub->position.y = y;
3443 sub->position.set = 1;
3444}
3445
3446static struct weston_subsurface *
3447subsurface_from_surface(struct weston_surface *surface)
3448{
3449 struct weston_subsurface *sub;
3450
3451 sub = weston_surface_to_subsurface(surface);
3452 if (sub)
3453 return sub;
3454
3455 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3456 if (sub->surface == surface)
3457 return sub;
3458
3459 return NULL;
3460}
3461
3462static struct weston_subsurface *
3463subsurface_sibling_check(struct weston_subsurface *sub,
3464 struct weston_surface *surface,
3465 const char *request)
3466{
3467 struct weston_subsurface *sibling;
3468
3469 sibling = subsurface_from_surface(surface);
3470
3471 if (!sibling) {
3472 wl_resource_post_error(sub->resource,
3473 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3474 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003475 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003476 return NULL;
3477 }
3478
3479 if (sibling->parent != sub->parent) {
3480 wl_resource_post_error(sub->resource,
3481 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3482 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003483 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003484 return NULL;
3485 }
3486
3487 return sibling;
3488}
3489
3490static void
3491subsurface_place_above(struct wl_client *client,
3492 struct wl_resource *resource,
3493 struct wl_resource *sibling_resource)
3494{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003495 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003496 struct weston_surface *surface =
3497 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003498 struct weston_subsurface *sibling;
3499
3500 if (!sub)
3501 return;
3502
3503 sibling = subsurface_sibling_check(sub, surface, "place_above");
3504 if (!sibling)
3505 return;
3506
3507 wl_list_remove(&sub->parent_link_pending);
3508 wl_list_insert(sibling->parent_link_pending.prev,
3509 &sub->parent_link_pending);
3510}
3511
3512static void
3513subsurface_place_below(struct wl_client *client,
3514 struct wl_resource *resource,
3515 struct wl_resource *sibling_resource)
3516{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003517 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003518 struct weston_surface *surface =
3519 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003520 struct weston_subsurface *sibling;
3521
3522 if (!sub)
3523 return;
3524
3525 sibling = subsurface_sibling_check(sub, surface, "place_below");
3526 if (!sibling)
3527 return;
3528
3529 wl_list_remove(&sub->parent_link_pending);
3530 wl_list_insert(&sibling->parent_link_pending,
3531 &sub->parent_link_pending);
3532}
3533
3534static void
3535subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3536{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003537 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003538
3539 if (sub)
3540 sub->synchronized = 1;
3541}
3542
3543static void
3544subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3545{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003546 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003547
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003548 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003549 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003550
3551 /* If sub became effectively desynchronized, flush. */
3552 if (!weston_subsurface_is_synchronized(sub))
3553 weston_subsurface_synchronized_commit(sub);
3554 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003555}
3556
3557static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003558weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3559{
3560 wl_list_remove(&sub->parent_link);
3561 wl_list_remove(&sub->parent_link_pending);
3562 wl_list_remove(&sub->parent_destroy_listener.link);
3563 sub->parent = NULL;
3564}
3565
3566static void
3567weston_subsurface_destroy(struct weston_subsurface *sub);
3568
3569static void
3570subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3571{
3572 struct weston_subsurface *sub =
3573 container_of(listener, struct weston_subsurface,
3574 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003575 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003576
3577 /* The protocol object (wl_resource) is left inert. */
3578 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003579 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003580
3581 weston_subsurface_destroy(sub);
3582}
3583
3584static void
3585subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3586{
3587 struct weston_subsurface *sub =
3588 container_of(listener, struct weston_subsurface,
3589 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003590 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003591 assert(sub->surface != sub->parent);
3592
3593 if (weston_surface_is_mapped(sub->surface))
3594 weston_surface_unmap(sub->surface);
3595
3596 weston_subsurface_unlink_parent(sub);
3597}
3598
3599static void
3600subsurface_resource_destroy(struct wl_resource *resource)
3601{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003602 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003603
3604 if (sub)
3605 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003606}
3607
3608static void
3609subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3610{
3611 wl_resource_destroy(resource);
3612}
3613
3614static void
3615weston_subsurface_link_parent(struct weston_subsurface *sub,
3616 struct weston_surface *parent)
3617{
3618 sub->parent = parent;
3619 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003620 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003621 &sub->parent_destroy_listener);
3622
3623 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3624 wl_list_insert(&parent->subsurface_list_pending,
3625 &sub->parent_link_pending);
3626}
3627
3628static void
3629weston_subsurface_link_surface(struct weston_subsurface *sub,
3630 struct weston_surface *surface)
3631{
3632 sub->surface = surface;
3633 sub->surface_destroy_listener.notify =
3634 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003635 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003636 &sub->surface_destroy_listener);
3637}
3638
3639static void
3640weston_subsurface_destroy(struct weston_subsurface *sub)
3641{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003642 struct weston_view *view, *next;
3643
Pekka Paalanene67858b2013-04-25 13:57:42 +03003644 assert(sub->surface);
3645
3646 if (sub->resource) {
3647 assert(weston_surface_to_subsurface(sub->surface) == sub);
3648 assert(sub->parent_destroy_listener.notify ==
3649 subsurface_handle_parent_destroy);
3650
George Kiagiadakised04d382014-06-13 18:10:26 +02003651 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3652 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003653 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003654 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003655
Pekka Paalanene67858b2013-04-25 13:57:42 +03003656 if (sub->parent)
3657 weston_subsurface_unlink_parent(sub);
3658
Jason Ekstrand7b982072014-05-20 14:33:03 -05003659 weston_surface_state_fini(&sub->cached);
3660 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003661
3662 sub->surface->configure = NULL;
3663 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003664 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003665 } else {
3666 /* the dummy weston_subsurface for the parent itself */
3667 assert(sub->parent_destroy_listener.notify == NULL);
3668 wl_list_remove(&sub->parent_link);
3669 wl_list_remove(&sub->parent_link_pending);
3670 }
3671
3672 wl_list_remove(&sub->surface_destroy_listener.link);
3673 free(sub);
3674}
3675
3676static const struct wl_subsurface_interface subsurface_implementation = {
3677 subsurface_destroy,
3678 subsurface_set_position,
3679 subsurface_place_above,
3680 subsurface_place_below,
3681 subsurface_set_sync,
3682 subsurface_set_desync
3683};
3684
3685static struct weston_subsurface *
3686weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3687 struct weston_surface *parent)
3688{
3689 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003690 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003691
Bryce Harringtonde16d892014-11-20 22:21:57 -08003692 sub = zalloc(sizeof *sub);
3693 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003694 return NULL;
3695
Jason Ekstranda7af7042013-10-12 22:38:11 -05003696 wl_list_init(&sub->unused_views);
3697
Jason Ekstranda85118c2013-06-27 20:17:02 -05003698 sub->resource =
3699 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003700 if (!sub->resource) {
3701 free(sub);
3702 return NULL;
3703 }
3704
Jason Ekstranda85118c2013-06-27 20:17:02 -05003705 wl_resource_set_implementation(sub->resource,
3706 &subsurface_implementation,
3707 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003708 weston_subsurface_link_surface(sub, surface);
3709 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003710 weston_surface_state_init(&sub->cached);
3711 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003712 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003713
3714 return sub;
3715}
3716
3717/* Create a dummy subsurface for having the parent itself in its
3718 * sub-surface lists. Makes stacking order manipulation easy.
3719 */
3720static struct weston_subsurface *
3721weston_subsurface_create_for_parent(struct weston_surface *parent)
3722{
3723 struct weston_subsurface *sub;
3724
Bryce Harringtonde16d892014-11-20 22:21:57 -08003725 sub = zalloc(sizeof *sub);
3726 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003727 return NULL;
3728
3729 weston_subsurface_link_surface(sub, parent);
3730 sub->parent = parent;
3731 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3732 wl_list_insert(&parent->subsurface_list_pending,
3733 &sub->parent_link_pending);
3734
3735 return sub;
3736}
3737
3738static void
3739subcompositor_get_subsurface(struct wl_client *client,
3740 struct wl_resource *resource,
3741 uint32_t id,
3742 struct wl_resource *surface_resource,
3743 struct wl_resource *parent_resource)
3744{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003745 struct weston_surface *surface =
3746 wl_resource_get_user_data(surface_resource);
3747 struct weston_surface *parent =
3748 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003749 struct weston_subsurface *sub;
3750 static const char where[] = "get_subsurface: wl_subsurface@";
3751
3752 if (surface == parent) {
3753 wl_resource_post_error(resource,
3754 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3755 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003756 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003757 return;
3758 }
3759
3760 if (weston_surface_to_subsurface(surface)) {
3761 wl_resource_post_error(resource,
3762 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3763 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003764 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003765 return;
3766 }
3767
Pekka Paalanen50b67472014-10-01 15:02:41 +03003768 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3769 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003770 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003771
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003772 if (weston_surface_get_main_surface(parent) == surface) {
3773 wl_resource_post_error(resource,
3774 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3775 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003776 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003777 return;
3778 }
3779
Pekka Paalanene67858b2013-04-25 13:57:42 +03003780 /* make sure the parent is in its own list */
3781 if (wl_list_empty(&parent->subsurface_list)) {
3782 if (!weston_subsurface_create_for_parent(parent)) {
3783 wl_resource_post_no_memory(resource);
3784 return;
3785 }
3786 }
3787
3788 sub = weston_subsurface_create(id, surface, parent);
3789 if (!sub) {
3790 wl_resource_post_no_memory(resource);
3791 return;
3792 }
3793
3794 surface->configure = subsurface_configure;
3795 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003796 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003797}
3798
3799static void
3800subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3801{
3802 wl_resource_destroy(resource);
3803}
3804
3805static const struct wl_subcompositor_interface subcompositor_interface = {
3806 subcompositor_destroy,
3807 subcompositor_get_subsurface
3808};
3809
3810static void
3811bind_subcompositor(struct wl_client *client,
3812 void *data, uint32_t version, uint32_t id)
3813{
3814 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003815 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003816
Jason Ekstranda85118c2013-06-27 20:17:02 -05003817 resource =
3818 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003819 if (resource == NULL) {
3820 wl_client_post_no_memory(client);
3821 return;
3822 }
3823 wl_resource_set_implementation(resource, &subcompositor_interface,
3824 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003825}
3826
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003827/** Set a DPMS mode on all of the compositor's outputs
3828 *
3829 * \param compositor The compositor instance
3830 * \param state The DPMS state the outputs will be set to
3831 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003832static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003833weston_compositor_dpms(struct weston_compositor *compositor,
3834 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003835{
3836 struct weston_output *output;
3837
3838 wl_list_for_each(output, &compositor->output_list, link)
3839 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003840 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003841}
3842
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003843/** Restores the compositor to active status
3844 *
3845 * \param compositor The compositor instance
3846 *
3847 * If the compositor was in a sleeping mode, all outputs are powered
3848 * back on via DPMS. Otherwise if the compositor was inactive
3849 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3850 * signal will fire.
3851 *
3852 * Restarts the idle timer.
3853 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003854WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003855weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003856{
Neil Roberts8b62e202013-09-30 13:14:47 +01003857 uint32_t old_state = compositor->state;
3858
3859 /* The state needs to be changed before emitting the wake
3860 * signal because that may try to schedule a repaint which
3861 * will not work if the compositor is still sleeping */
3862 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3863
3864 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003865 case WESTON_COMPOSITOR_SLEEPING:
3866 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3867 /* fall through */
3868 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003869 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003870 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003871 /* fall through */
3872 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003873 wl_event_source_timer_update(compositor->idle_source,
3874 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003875 }
3876}
3877
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003878/** Turns off rendering and frame events for the compositor.
3879 *
3880 * \param compositor The compositor instance
3881 *
3882 * This is used for example to prevent further rendering while the
3883 * compositor is shutting down.
3884 *
3885 * \note When offscreen state is entered, outputs will be powered
3886 * back on if they were sleeping (in DPMS off mode), even though
3887 * no rendering will be performed.
3888 *
3889 * Stops the idle timer.
3890 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003891WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003892weston_compositor_offscreen(struct weston_compositor *compositor)
3893{
3894 switch (compositor->state) {
3895 case WESTON_COMPOSITOR_OFFSCREEN:
3896 return;
3897 case WESTON_COMPOSITOR_SLEEPING:
3898 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3899 /* fall through */
3900 default:
3901 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3902 wl_event_source_timer_update(compositor->idle_source, 0);
3903 }
3904}
3905
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003906/** Powers down all attached output devices
3907 *
3908 * \param compositor The compositor instance
3909 *
3910 * Causes rendering to the outputs to cease, and no frame events to be
3911 * sent. Only powers down the outputs if the compositor is not already
3912 * in sleep mode.
3913 *
3914 * Stops the idle timer.
3915 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003916WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003917weston_compositor_sleep(struct weston_compositor *compositor)
3918{
3919 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3920 return;
3921
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003922 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003923 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3924 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3925}
3926
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003927/** Sets compositor to idle mode
3928 *
3929 * \param data The compositor instance
3930 *
3931 * This is called when the idle timer fires. Once the compositor is in
3932 * idle mode it requires a wake action (e.g. via
3933 * weston_compositor_wake()) to restore it. The compositor's
3934 * idle_signal will be triggered when the idle event occurs.
3935 *
3936 * Idleness can be inhibited by setting the compositor's idle_inhibit
3937 * property.
3938 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003939static int
3940idle_handler(void *data)
3941{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003942 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003943
3944 if (compositor->idle_inhibit)
3945 return 1;
3946
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003947 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003948 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003949
3950 return 1;
3951}
3952
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003953WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003954weston_plane_init(struct weston_plane *plane,
3955 struct weston_compositor *ec,
3956 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003957{
3958 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003959 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003960 plane->x = x;
3961 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003962 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003963
3964 /* Init the link so that the call to wl_list_remove() when releasing
3965 * the plane without ever stacking doesn't lead to a crash */
3966 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003967}
3968
3969WL_EXPORT void
3970weston_plane_release(struct weston_plane *plane)
3971{
Xiong Zhang97116532013-10-23 13:58:31 +08003972 struct weston_view *view;
3973
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003974 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003975 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003976
Xiong Zhang97116532013-10-23 13:58:31 +08003977 wl_list_for_each(view, &plane->compositor->view_list, link) {
3978 if (view->plane == plane)
3979 view->plane = NULL;
3980 }
3981
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003982 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003983}
3984
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003985WL_EXPORT void
3986weston_compositor_stack_plane(struct weston_compositor *ec,
3987 struct weston_plane *plane,
3988 struct weston_plane *above)
3989{
3990 if (above)
3991 wl_list_insert(above->link.prev, &plane->link);
3992 else
3993 wl_list_insert(&ec->plane_list, &plane->link);
3994}
3995
Casey Dahlin9074db52012-04-19 22:50:09 -04003996static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003997{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003998 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003999}
4000
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004001static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004002bind_output(struct wl_client *client,
4003 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004004{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004005 struct weston_output *output = data;
4006 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004007 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004008
Jason Ekstranda85118c2013-06-27 20:17:02 -05004009 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004010 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004011 if (resource == NULL) {
4012 wl_client_post_no_memory(client);
4013 return;
4014 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004015
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004016 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05004017 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004018
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004019 wl_output_send_geometry(resource,
4020 output->x,
4021 output->y,
4022 output->mm_width,
4023 output->mm_height,
4024 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004025 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004026 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004027 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004028 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004029 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004030
4031 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004032 wl_output_send_mode(resource,
4033 mode->flags,
4034 mode->width,
4035 mode->height,
4036 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004037 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004038
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004039 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004040 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004041}
4042
David Fort0de859e2016-05-27 23:22:57 +02004043/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004044static void
David Fort0de859e2016-05-27 23:22:57 +02004045weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4046 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004047{
4048 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004049 bool start_resizing = false;
4050
4051 if (!delta_width)
4052 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004053
4054 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004055 if (output == resized_output) {
4056 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004057 continue;
4058 }
4059
David Fort0de859e2016-05-27 23:22:57 +02004060 if (start_resizing) {
4061 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004062 output->dirty = 1;
4063 }
4064 }
4065}
4066
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004067WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004068weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004069{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004070 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004071 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004072
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004073 output->destroying = 1;
4074
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004075 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004076 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004077 weston_view_assign_output(view);
4078 }
4079
Pekka Paalanen0513a952014-05-21 16:17:27 +03004080 wl_event_source_remove(output->repaint_timer);
4081
Pekka Paalanen133e4392014-09-23 22:08:46 -04004082 weston_presentation_feedback_discard_list(&output->feedback_list);
4083
David Fort0de859e2016-05-27 23:22:57 +02004084 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004085 wl_list_remove(&output->link);
4086
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004087 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004088 wl_signal_emit(&output->destroy_signal, output);
4089
Richard Hughesafe690c2013-05-02 10:10:04 +01004090 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004091 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004092 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004093 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004094
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004095 wl_resource_for_each(resource, &output->resource_list) {
4096 wl_resource_set_destructor(resource, NULL);
4097 }
4098
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004099 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004100}
4101
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004102WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004103weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004104{
Scott Moreau850ca422012-05-21 15:21:25 -06004105 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004106
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004107 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004108 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004109
Scott Moreauccbf29d2012-02-22 14:21:41 -07004110 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004111 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004112 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004113 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004114 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004115 weston_matrix_scale(&output->matrix, magnification,
4116 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004117 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004118
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004119 switch (output->transform) {
4120 case WL_OUTPUT_TRANSFORM_FLIPPED:
4121 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4122 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4123 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4124 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4125 weston_matrix_scale(&output->matrix, -1, 1, 1);
4126 break;
4127 }
4128
4129 switch (output->transform) {
4130 default:
4131 case WL_OUTPUT_TRANSFORM_NORMAL:
4132 case WL_OUTPUT_TRANSFORM_FLIPPED:
4133 break;
4134 case WL_OUTPUT_TRANSFORM_90:
4135 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4136 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4137 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4138 break;
4139 case WL_OUTPUT_TRANSFORM_180:
4140 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4141 weston_matrix_translate(&output->matrix,
4142 -output->width, -output->height, 0);
4143 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4144 break;
4145 case WL_OUTPUT_TRANSFORM_270:
4146 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4147 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4148 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4149 break;
4150 }
4151
4152 if (output->current_scale != 1)
4153 weston_matrix_scale(&output->matrix,
4154 output->current_scale,
4155 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004156
Scott Moreauccbf29d2012-02-22 14:21:41 -07004157 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004158
4159 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004160}
4161
Scott Moreau1bad5db2012-08-18 01:04:05 -06004162static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004163weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004164{
4165 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004166 output->native_scale = scale;
4167 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004168
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004169 convert_size_by_transform_scale(&output->width, &output->height,
4170 output->current_mode->width,
4171 output->current_mode->height,
4172 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004173}
4174
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004175static void
4176weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004177{
4178 output->x = x;
4179 output->y = y;
4180
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004181 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004182 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004183 output->width,
4184 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004185}
4186
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004187WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004188weston_output_move(struct weston_output *output, int x, int y)
4189{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004190 struct wl_resource *resource;
4191
4192 output->move_x = x - output->x;
4193 output->move_y = y - output->y;
4194
4195 if (output->move_x == 0 && output->move_y == 0)
4196 return;
4197
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004198 weston_output_init_geometry(output, x, y);
4199
4200 output->dirty = 1;
4201
4202 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004203 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004204
4205 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004206 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004207 wl_output_send_geometry(resource,
4208 output->x,
4209 output->y,
4210 output->mm_width,
4211 output->mm_height,
4212 output->subpixel,
4213 output->make,
4214 output->model,
4215 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004216
FORT David8a120692016-04-26 23:34:06 +02004217 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004218 wl_output_send_done(resource);
4219 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004220}
4221
Bryce Harrington3f650b82015-12-23 11:01:58 -08004222/** Initialize a weston_output object's parameters
4223 *
4224 * \param output The weston_output object to initialize
4225 * \param c The output's compositor
4226 * \param x x coordinate for the output in global coordinate space
4227 * \param y y coordinate for the output in global coordinate space
4228 * \param mm_width Physical width of the output as reported by the backend
4229 * \param mm_height Physical height of the output as reported by the backend
4230 * \param transform Rotation of the output
4231 * \param scale Native scaling factor for the output
4232 *
4233 * Sets up the transformation, zoom, and geometry of the output using
4234 * the input properties.
4235 *
4236 * Establishes a repaint timer for the output with the relevant display
4237 * object's event loop. See output_repaint_timer_handler().
4238 *
4239 * The output is assigned an ID. Weston can support up to 32 distinct
4240 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4241 * is referred to and used to find the first available ID number, and
4242 * then this ID is marked as used in output_id_pool.
4243 *
4244 * The output is also assigned a Wayland global with the wl_output
4245 * external interface.
4246 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004247WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004248weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004249 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004250 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004251{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004252 struct wl_event_loop *loop;
4253
Bryce Harrington18e45732015-12-23 20:53:53 -08004254 /* Verify we haven't reached the limit of 32 available output IDs */
4255 assert(ffs(~c->output_id_pool) > 0);
4256
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004257 output->compositor = c;
4258 output->x = x;
4259 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004260 output->mm_width = mm_width;
4261 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004262 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004263 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004264
Alexander Larsson0b135062013-05-28 16:23:36 +02004265 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004266 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004267
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004268 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004269 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004270
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004271 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004272 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004273 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004274 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004275 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004276 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004277
Pekka Paalanen0513a952014-05-21 16:17:27 +03004278 loop = wl_display_get_event_loop(c->wl_display);
4279 output->repaint_timer = wl_event_loop_add_timer(loop,
4280 output_repaint_timer_handler, output);
4281
Bryce Harrington3f650b82015-12-23 11:01:58 -08004282 /* Invert the output id pool and look for the lowest numbered
4283 * switch (the least significant bit). Take that bit's position
4284 * as our ID, and mark it used in the compositor's output_id_pool.
4285 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004286 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004287 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004288
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004289 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004290 wl_global_create(c->wl_display, &wl_output_interface, 2,
4291 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004292}
4293
4294/** Adds an output to the compositor's output list and
4295 * send the compositor's output_created signal.
4296 *
4297 * \param compositor The compositor instance.
4298 * \param output The output to be added.
4299 */
4300WL_EXPORT void
4301weston_compositor_add_output(struct weston_compositor *compositor,
4302 struct weston_output *output)
4303{
4304 wl_list_insert(compositor->output_list.prev, &output->link);
4305 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004306}
4307
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004308WL_EXPORT void
4309weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004310 double device_x, double device_y,
4311 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004312{
Derek Foreman0f679412014-10-02 13:41:17 -05004313 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004314 device_x,
4315 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004316 0.0,
4317 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004318
Derek Foreman67a18b92015-03-24 11:36:14 -05004319 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004320
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004321 *x = p.f[0] / p.f[3];
4322 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004323}
4324
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004325static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004326destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004327{
Jonny Lamb74130762013-11-26 18:19:46 +01004328 struct weston_surface *surface =
4329 wl_resource_get_user_data(resource);
4330
Pekka Paalanen4826f872016-04-22 14:14:38 +03004331 if (!surface)
4332 return;
4333
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004334 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004335 surface->pending.buffer_viewport.buffer.src_width =
4336 wl_fixed_from_int(-1);
4337 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004338 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004339}
4340
4341static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004342viewport_destroy(struct wl_client *client,
4343 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004344{
4345 wl_resource_destroy(resource);
4346}
4347
4348static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004349viewport_set_source(struct wl_client *client,
4350 struct wl_resource *resource,
4351 wl_fixed_t src_x,
4352 wl_fixed_t src_y,
4353 wl_fixed_t src_width,
4354 wl_fixed_t src_height)
4355{
4356 struct weston_surface *surface =
4357 wl_resource_get_user_data(resource);
4358
Pekka Paalanen4826f872016-04-22 14:14:38 +03004359 if (!surface) {
4360 wl_resource_post_error(resource,
4361 WP_VIEWPORT_ERROR_NO_SURFACE,
4362 "wl_surface for this viewport is no longer exists");
4363 return;
4364 }
4365
4366 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004367 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004368
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004369 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004370 src_height == wl_fixed_from_int(-1) &&
4371 src_x == wl_fixed_from_int(-1) &&
4372 src_y == wl_fixed_from_int(-1)) {
4373 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004374 surface->pending.buffer_viewport.buffer.src_width =
4375 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004376 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004377 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004378 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004379
Pekka Paalanen201769a2016-04-26 14:42:11 +03004380 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004381 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004382 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004383 "wl_surface@%d viewport source "
4384 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4385 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004386 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004387 wl_fixed_to_double(src_height),
4388 wl_fixed_to_double(src_x),
4389 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004390 return;
4391 }
4392
4393 surface->pending.buffer_viewport.buffer.src_x = src_x;
4394 surface->pending.buffer_viewport.buffer.src_y = src_y;
4395 surface->pending.buffer_viewport.buffer.src_width = src_width;
4396 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004397 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004398}
4399
4400static void
4401viewport_set_destination(struct wl_client *client,
4402 struct wl_resource *resource,
4403 int32_t dst_width,
4404 int32_t dst_height)
4405{
4406 struct weston_surface *surface =
4407 wl_resource_get_user_data(resource);
4408
Pekka Paalanen4826f872016-04-22 14:14:38 +03004409 if (!surface) {
4410 wl_resource_post_error(resource,
4411 WP_VIEWPORT_ERROR_NO_SURFACE,
4412 "wl_surface for this viewport no longer exists");
4413 return;
4414 }
4415
4416 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004417
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004418 if (dst_width == -1 && dst_height == -1) {
4419 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004420 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004421 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004422 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004423 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004424
4425 if (dst_width <= 0 || dst_height <= 0) {
4426 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004427 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004428 "destination size must be positive (%dx%d)",
4429 dst_width, dst_height);
4430 return;
4431 }
4432
4433 surface->pending.buffer_viewport.surface.width = dst_width;
4434 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004435 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004436}
4437
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004438static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004439 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004440 viewport_set_source,
4441 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004442};
4443
4444static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004445viewporter_destroy(struct wl_client *client,
4446 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004447{
4448 wl_resource_destroy(resource);
4449}
4450
4451static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004452viewporter_get_viewport(struct wl_client *client,
4453 struct wl_resource *viewporter,
4454 uint32_t id,
4455 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004456{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004457 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004458 struct weston_surface *surface =
4459 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004460 struct wl_resource *resource;
4461
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004462 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004463 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004464 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004465 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004466 return;
4467 }
4468
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004469 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004470 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004471 if (resource == NULL) {
4472 wl_client_post_no_memory(client);
4473 return;
4474 }
4475
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004476 wl_resource_set_implementation(resource, &viewport_interface,
4477 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004478
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004479 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004480}
4481
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004482static const struct wp_viewporter_interface viewporter_interface = {
4483 viewporter_destroy,
4484 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004485};
4486
4487static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004488bind_viewporter(struct wl_client *client,
4489 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004490{
4491 struct wl_resource *resource;
4492
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004493 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004494 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004495 if (resource == NULL) {
4496 wl_client_post_no_memory(client);
4497 return;
4498 }
4499
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004500 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004501 NULL, NULL);
4502}
4503
4504static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004505destroy_presentation_feedback(struct wl_resource *feedback_resource)
4506{
4507 struct weston_presentation_feedback *feedback;
4508
4509 feedback = wl_resource_get_user_data(feedback_resource);
4510
4511 wl_list_remove(&feedback->link);
4512 free(feedback);
4513}
4514
4515static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004516presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4517{
4518 wl_resource_destroy(resource);
4519}
4520
4521static void
4522presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004523 struct wl_resource *presentation_resource,
4524 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004525 uint32_t callback)
4526{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004527 struct weston_surface *surface;
4528 struct weston_presentation_feedback *feedback;
4529
4530 surface = wl_resource_get_user_data(surface_resource);
4531
Bryce Harringtonde16d892014-11-20 22:21:57 -08004532 feedback = zalloc(sizeof *feedback);
4533 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004534 goto err_calloc;
4535
4536 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004537 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004538 1, callback);
4539 if (!feedback->resource)
4540 goto err_create;
4541
4542 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4543 destroy_presentation_feedback);
4544
4545 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4546
4547 return;
4548
4549err_create:
4550 free(feedback);
4551
4552err_calloc:
4553 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004554}
4555
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004556static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004557 presentation_destroy,
4558 presentation_feedback
4559};
4560
4561static void
4562bind_presentation(struct wl_client *client,
4563 void *data, uint32_t version, uint32_t id)
4564{
4565 struct weston_compositor *compositor = data;
4566 struct wl_resource *resource;
4567
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004568 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004569 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004570 if (resource == NULL) {
4571 wl_client_post_no_memory(client);
4572 return;
4573 }
4574
4575 wl_resource_set_implementation(resource, &presentation_implementation,
4576 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004577 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004578}
4579
4580static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004581compositor_bind(struct wl_client *client,
4582 void *data, uint32_t version, uint32_t id)
4583{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004584 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004585 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004586
Jason Ekstranda85118c2013-06-27 20:17:02 -05004587 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004588 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004589 if (resource == NULL) {
4590 wl_client_post_no_memory(client);
4591 return;
4592 }
4593
4594 wl_resource_set_implementation(resource, &compositor_interface,
4595 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004596}
4597
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004598WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004599weston_environment_get_fd(const char *env)
4600{
4601 char *e, *end;
4602 int fd, flags;
4603
4604 e = getenv(env);
4605 if (!e)
4606 return -1;
4607 fd = strtol(e, &end, 0);
4608 if (*end != '\0')
4609 return -1;
4610
4611 flags = fcntl(fd, F_GETFD);
4612 if (flags == -1)
4613 return -1;
4614
4615 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4616 unsetenv(env);
4617
4618 return fd;
4619}
4620
Pekka Paalanenb5026542014-11-12 15:09:24 +02004621static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004622timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004623 uint32_t key, void *data)
4624{
4625 struct weston_compositor *compositor = data;
4626
4627 if (weston_timeline_enabled_)
4628 weston_timeline_close();
4629 else
4630 weston_timeline_open(compositor);
4631}
4632
Giulio Camuffo459137b2014-10-11 23:56:24 +03004633/** Create the compositor.
4634 *
4635 * This functions creates and initializes a compositor instance.
4636 *
4637 * \param display The Wayland display to be used.
4638 * \param user_data A pointer to an object that can later be retrieved
4639 * using the \ref weston_compositor_get_user_data function.
4640 * \return The compositor instance on success or NULL on failure.
4641 */
4642WL_EXPORT struct weston_compositor *
4643weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004644{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004645 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004646 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004647
Giulio Camuffo459137b2014-10-11 23:56:24 +03004648 ec = zalloc(sizeof *ec);
4649 if (!ec)
4650 return NULL;
4651
4652 ec->wl_display = display;
4653 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004654 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004655 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004656 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004657 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004658 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004659 wl_signal_init(&ec->idle_signal);
4660 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004661 wl_signal_init(&ec->show_input_panel_signal);
4662 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004663 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004664 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004665 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004666 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004667 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004668 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004669 wl_signal_init(&ec->session_signal);
4670 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004671
Casey Dahlin58ba1372012-04-19 22:50:08 -04004672 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004673 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004674
Derek Foreman152254b2015-11-26 14:17:48 -06004675 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004676 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004677 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004678
Giulio Camuffo954f1832014-10-11 18:27:30 +03004679 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004680 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004681 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004682
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004683 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004684 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004685 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004686
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004687 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004688 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004689 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004690
Jason Ekstranda7af7042013-10-12 22:38:11 -05004691 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004692 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004693 wl_list_init(&ec->layer_list);
4694 wl_list_init(&ec->seat_list);
4695 wl_list_init(&ec->output_list);
4696 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004697 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004698 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004699 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004700 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004701 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004702
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004703 wl_list_init(&ec->plugin_api_list);
4704
Xiong Zhang97116532013-10-23 13:58:31 +08004705 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004706 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004707
Giulio Camuffo459137b2014-10-11 23:56:24 +03004708 wl_data_device_manager_init(ec->wl_display);
4709
4710 wl_display_init_shm(ec->wl_display);
4711
4712 loop = wl_display_get_event_loop(ec->wl_display);
4713 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004714
Giulio Camuffo459137b2014-10-11 23:56:24 +03004715 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4716 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4717
4718 weston_compositor_add_debug_binding(ec, KEY_T,
4719 timeline_key_binding_handler, ec);
4720
Giulio Camuffo459137b2014-10-11 23:56:24 +03004721 return ec;
4722
4723fail:
4724 free(ec);
4725 return NULL;
4726}
4727
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004728WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004729weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004730{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004731 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004732
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004733 wl_event_source_remove(ec->idle_source);
4734
Matt Roper361d2ad2011-08-29 13:52:23 -07004735 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004736 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004737 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004738
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004739 if (ec->renderer)
4740 ec->renderer->destroy(ec);
4741
Daniel Stone325fc2d2012-05-30 16:31:58 +01004742 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004743 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004744 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004745 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004746 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004747 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004748
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004749 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004750}
4751
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004752WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004753weston_compositor_exit_with_code(struct weston_compositor *compositor,
4754 int exit_code)
4755{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004756 if (compositor->exit_code == EXIT_SUCCESS)
4757 compositor->exit_code = exit_code;
4758
Giulio Camuffo459137b2014-10-11 23:56:24 +03004759 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004760}
4761
4762WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004763weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4764 const struct weston_pointer_grab_interface *interface)
4765{
4766 struct weston_seat *seat;
4767
4768 ec->default_pointer_grab = interface;
4769 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004770 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4771
4772 if (pointer)
4773 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004774 }
4775}
4776
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004777WL_EXPORT int
4778weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4779 clockid_t clk_id)
4780{
4781 struct timespec ts;
4782
4783 if (clock_gettime(clk_id, &ts) < 0)
4784 return -1;
4785
4786 compositor->presentation_clock = clk_id;
4787
4788 return 0;
4789}
4790
4791/*
4792 * For choosing the software clock, when the display hardware or API
4793 * does not expose a compatible presentation timestamp.
4794 */
4795WL_EXPORT int
4796weston_compositor_set_presentation_clock_software(
4797 struct weston_compositor *compositor)
4798{
4799 /* In order of preference */
4800 static const clockid_t clocks[] = {
4801 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4802 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4803 CLOCK_MONOTONIC, /* no jumps, may crawl */
4804 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4805 CLOCK_REALTIME /* may jump and crawl */
4806 };
4807 unsigned i;
4808
4809 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4810 if (weston_compositor_set_presentation_clock(compositor,
4811 clocks[i]) == 0)
4812 return 0;
4813
4814 weston_log("Error: no suitable presentation clock available.\n");
4815
4816 return -1;
4817}
4818
Pekka Paalanen662f3842015-03-18 12:17:26 +02004819/** Read the current time from the Presentation clock
4820 *
4821 * \param compositor
4822 * \param ts[out] The current time.
4823 *
4824 * \note Reading the current time in user space is always imprecise to some
4825 * degree.
4826 *
4827 * This function is never meant to fail. If reading the clock does fail,
4828 * an error message is logged and a zero time is returned. Callers are not
4829 * supposed to detect or react to failures.
4830 */
4831WL_EXPORT void
4832weston_compositor_read_presentation_clock(
4833 const struct weston_compositor *compositor,
4834 struct timespec *ts)
4835{
4836 static bool warned;
4837 int ret;
4838
4839 ret = clock_gettime(compositor->presentation_clock, ts);
4840 if (ret < 0) {
4841 ts->tv_sec = 0;
4842 ts->tv_nsec = 0;
4843
4844 if (!warned)
4845 weston_log("Error: failure to read "
4846 "the presentation clock %#x: '%m' (%d)\n",
4847 compositor->presentation_clock, errno);
4848 warned = true;
4849 }
4850}
4851
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004852/** Import dmabuf buffer into current renderer
4853 *
4854 * \param compositor
4855 * \param buffer the dmabuf buffer to import
4856 * \return true on usable buffers, false otherwise
4857 *
4858 * This function tests that the linux_dmabuf_buffer is usable
4859 * for the current renderer. Returns false on unusable buffers. Usually
4860 * usability is tested by importing the dmabufs for composition.
4861 *
4862 * This hook is also used for detecting if the renderer supports
4863 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4864 * supported.
4865 * */
4866WL_EXPORT bool
4867weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4868 struct linux_dmabuf_buffer *buffer)
4869{
4870 struct weston_renderer *renderer;
4871
4872 renderer = compositor->renderer;
4873
4874 if (renderer->import_dmabuf == NULL)
4875 return false;
4876
4877 return renderer->import_dmabuf(compositor, buffer);
4878}
4879
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004880WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004881weston_version(int *major, int *minor, int *micro)
4882{
4883 *major = WESTON_VERSION_MAJOR;
4884 *minor = WESTON_VERSION_MINOR;
4885 *micro = WESTON_VERSION_MICRO;
4886}
4887
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004888WL_EXPORT void *
4889weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004890{
Derek Foreman3f86e502015-06-08 11:46:54 -05004891 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004892 char path[PATH_MAX];
4893 void *module, *init;
4894
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004895 if (name == NULL)
4896 return NULL;
4897
Derek Foreman3f86e502015-06-08 11:46:54 -05004898 if (name[0] != '/') {
4899 if (builddir)
4900 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4901 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03004902 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004903 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004904 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004905 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004906
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004907 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4908 if (module) {
4909 weston_log("Module '%s' already loaded\n", path);
4910 dlclose(module);
4911 return NULL;
4912 }
4913
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004914 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004915 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004916 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004917 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004918 return NULL;
4919 }
4920
4921 init = dlsym(module, entrypoint);
4922 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004923 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004924 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004925 return NULL;
4926 }
4927
4928 return init;
4929}
4930
Giulio Camuffo459137b2014-10-11 23:56:24 +03004931
4932/** Destroys the compositor.
4933 *
4934 * This function cleans up the compositor state and destroys it.
4935 *
4936 * \param compositor The compositor to be destroyed.
4937 */
4938WL_EXPORT void
4939weston_compositor_destroy(struct weston_compositor *compositor)
4940{
4941 /* prevent further rendering while shutting down */
4942 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4943
4944 wl_signal_emit(&compositor->destroy_signal, compositor);
4945
4946 weston_compositor_xkb_destroy(compositor);
4947
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004948 if (compositor->backend)
4949 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02004950
4951 weston_plugin_api_destroy_list(compositor);
4952
Giulio Camuffo459137b2014-10-11 23:56:24 +03004953 free(compositor);
4954}
4955
4956/** Instruct the compositor to exit.
4957 *
4958 * This functions does not directly destroy the compositor object, it merely
4959 * command it to start the tear down process. It is not guaranteed that the
4960 * tear down will happen immediately.
4961 *
4962 * \param compositor The compositor to tear down.
4963 */
4964WL_EXPORT void
4965weston_compositor_exit(struct weston_compositor *compositor)
4966{
4967 compositor->exit(compositor);
4968}
4969
4970/** Return the user data stored in the compositor.
4971 *
4972 * This function returns the user data pointer set with user_data parameter
4973 * to the \ref weston_compositor_create function.
4974 */
4975WL_EXPORT void *
4976weston_compositor_get_user_data(struct weston_compositor *compositor)
4977{
4978 return compositor->user_data;
4979}
Pekka Paalanendd186732016-06-03 14:49:54 +03004980
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004981static const char * const backend_map[] = {
4982 [WESTON_BACKEND_DRM] = "drm-backend.so",
4983 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
4984 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
4985 [WESTON_BACKEND_RDP] = "rdp-backend.so",
4986 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
4987 [WESTON_BACKEND_X11] = "x11-backend.so",
4988};
4989
Pekka Paalanendd186732016-06-03 14:49:54 +03004990/** Load a backend into a weston_compositor
4991 *
4992 * A backend must be loaded to make a weston_compositor work. A backend
4993 * provides input and output capabilities, and determines the renderer to use.
4994 *
4995 * \param compositor A compositor that has not had a backend loaded yet.
4996 * \param backend Name of the backend file.
4997 * \param config_base A pointer to a backend-specific configuration
4998 * structure's 'base' member.
4999 *
5000 * \return 0 on success, or -1 on error.
5001 */
5002WL_EXPORT int
5003weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005004 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005005 struct weston_backend_config *config_base)
5006{
5007 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005008 struct weston_backend_config *config_base);
5009
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005010 if (backend < 0 || backend >= ARRAY_LENGTH(backend_map))
5011 return -1;
5012
5013 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005014 if (!backend_init)
5015 return -1;
5016
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005017 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005018}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005019
5020WL_EXPORT int
5021weston_compositor_load_xwayland(struct weston_compositor *compositor)
5022{
5023 int (*module_init)(struct weston_compositor *ec,
5024 int *argc, char *argv[]);
5025 int argc = 0;
5026
5027 module_init = weston_load_module("xwayland.so", "module_init");
5028 if (!module_init)
5029 return -1;
5030 if (module_init(compositor, &argc, NULL) < 0)
5031 return -1;
5032 return 0;
5033}