blob: f410214d7430b250671b7eded19769e24ec5a8f9 [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"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064
Pekka Paalanen0513a952014-05-21 16:17:27 +030065#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
66
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020067static void
Alexander Larsson0b135062013-05-28 16:23:36 +020068weston_output_transform_scale_init(struct weston_output *output,
69 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020070
Rob Bradford27b17932013-06-26 18:08:46 +010071static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050072weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010073
Derek Foreman6ae7bc92014-11-04 10:47:33 -060074static void weston_mode_switch_finish(struct weston_output *output,
75 int mode_changed,
76 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080077{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020078 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020079 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020080 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060081 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020082
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020083 pixman_region32_init(&old_output_region);
84 pixman_region32_copy(&old_output_region, &output->region);
85
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020086 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020087 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088
89 pixman_region32_init(&output->previous_damage);
90 pixman_region32_init_rect(&output->region, output->x, output->y,
91 output->width, output->height);
92
93 weston_output_update_matrix(output);
94
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020095 /* If a pointer falls outside the outputs new geometry, move it to its
96 * lower-right corner */
97 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -050098 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020099 int32_t x, y;
100
101 if (!pointer)
102 continue;
103
104 x = wl_fixed_to_int(pointer->x);
105 y = wl_fixed_to_int(pointer->y);
106
107 if (!pixman_region32_contains_point(&old_output_region,
108 x, y, NULL) ||
109 pixman_region32_contains_point(&output->region,
110 x, y, NULL))
111 continue;
112
113 if (x >= output->x + output->width)
114 x = output->x + output->width - 1;
115 if (y >= output->y + output->height)
116 y = output->y + output->height - 1;
117
118 pointer->x = wl_fixed_from_int(x);
119 pointer->y = wl_fixed_from_int(y);
120 }
121
122 pixman_region32_fini(&old_output_region);
123
Derek Foremandd4cd332014-11-10 10:29:59 -0600124 if (!mode_changed && !scale_changed)
125 return;
126
Hardening57388e42013-09-18 23:56:36 +0200127 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600128 wl_resource_for_each(resource, &output->resource_list) {
129 if (mode_changed) {
130 wl_output_send_mode(resource,
131 output->current_mode->flags,
132 output->current_mode->width,
133 output->current_mode->height,
134 output->current_mode->refresh);
135 }
Hardening57388e42013-09-18 23:56:36 +0200136
Derek Foreman41bdc272014-11-05 13:26:57 -0600137 version = wl_resource_get_version(resource);
138 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600139 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200140
Derek Foreman41bdc272014-11-05 13:26:57 -0600141 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
142 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600143 }
144}
145
David Fort0de859e2016-05-27 23:22:57 +0200146
147static void
148weston_compositor_reflow_outputs(struct weston_compositor *compositor,
149 struct weston_output *resized_output, int delta_width);
150
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600151WL_EXPORT int
152weston_output_mode_set_native(struct weston_output *output,
153 struct weston_mode *mode,
154 int32_t scale)
155{
156 int ret;
157 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200158 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600159
160 if (!output->switch_mode)
161 return -1;
162
163 if (!output->original_mode) {
164 mode_changed = 1;
165 ret = output->switch_mode(output, mode);
166 if (ret < 0)
167 return ret;
168 if (output->current_scale != scale) {
169 scale_changed = 1;
170 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200171 }
172 }
173
David Fort0de859e2016-05-27 23:22:57 +0200174 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600175 output->native_mode = mode;
176 output->native_scale = scale;
177
178 weston_mode_switch_finish(output, mode_changed, scale_changed);
179
David Fort0de859e2016-05-27 23:22:57 +0200180 if (mode_changed || scale_changed) {
181 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
182
183 wl_signal_emit(&output->compositor->output_resized_signal, output);
184 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600185 return 0;
186}
187
188WL_EXPORT int
189weston_output_mode_switch_to_native(struct weston_output *output)
190{
191 int ret;
192 int mode_changed = 0, scale_changed = 0;
193
194 if (!output->switch_mode)
195 return -1;
196
197 if (!output->original_mode) {
198 weston_log("already in the native mode\n");
199 return -1;
200 }
201 /* the non fullscreen clients haven't seen a mode set since we
202 * switched into a temporary, so we need to notify them if the
203 * mode at that time is different from the native mode now.
204 */
205 mode_changed = (output->original_mode != output->native_mode);
206 scale_changed = (output->original_scale != output->native_scale);
207
208 ret = output->switch_mode(output, output->native_mode);
209 if (ret < 0)
210 return ret;
211
212 output->current_scale = output->native_scale;
213
214 output->original_mode = NULL;
215 output->original_scale = 0;
216
217 weston_mode_switch_finish(output, mode_changed, scale_changed);
218
219 return 0;
220}
221
222WL_EXPORT int
223weston_output_mode_switch_to_temporary(struct weston_output *output,
224 struct weston_mode *mode,
225 int32_t scale)
226{
227 int ret;
228
229 if (!output->switch_mode)
230 return -1;
231
232 /* original_mode is the last mode non full screen clients have seen,
233 * so we shouldn't change it if we already have one set.
234 */
235 if (!output->original_mode) {
236 output->original_mode = output->native_mode;
237 output->original_scale = output->native_scale;
238 }
239 ret = output->switch_mode(output, mode);
240 if (ret < 0)
241 return ret;
242
243 output->current_scale = scale;
244
245 weston_mode_switch_finish(output, 0, 0);
246
247 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800248}
249
Benjamin Franzke06286262011-05-06 19:12:33 +0200250static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300251region_init_infinite(pixman_region32_t *region)
252{
253 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
254 UINT32_MAX, UINT32_MAX);
255}
256
Pekka Paalanene67858b2013-04-25 13:57:42 +0300257static struct weston_subsurface *
258weston_surface_to_subsurface(struct weston_surface *surface);
259
Jason Ekstranda7af7042013-10-12 22:38:11 -0500260WL_EXPORT struct weston_view *
261weston_view_create(struct weston_surface *surface)
262{
263 struct weston_view *view;
264
Bryce Harringtonde16d892014-11-20 22:21:57 -0800265 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500266 if (view == NULL)
267 return NULL;
268
269 view->surface = surface;
270
Jason Ekstranda7af7042013-10-12 22:38:11 -0500271 /* Assign to surface */
272 wl_list_insert(&surface->views, &view->surface_link);
273
274 wl_signal_init(&view->destroy_signal);
275 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300276 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500277
Jason Ekstranda7af7042013-10-12 22:38:11 -0500278 pixman_region32_init(&view->clip);
279
280 view->alpha = 1.0;
281 pixman_region32_init(&view->transform.opaque);
282
283 wl_list_init(&view->geometry.transformation_list);
284 wl_list_insert(&view->geometry.transformation_list,
285 &view->transform.position.link);
286 weston_matrix_init(&view->transform.position.matrix);
287 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200288 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 pixman_region32_init(&view->transform.boundingbox);
290 view->transform.dirty = 1;
291
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292 return view;
293}
294
Jason Ekstrand108865d2014-06-26 10:04:49 -0700295struct weston_frame_callback {
296 struct wl_resource *resource;
297 struct wl_list link;
298};
299
Pekka Paalanen133e4392014-09-23 22:08:46 -0400300struct weston_presentation_feedback {
301 struct wl_resource *resource;
302
303 /* XXX: could use just wl_resource_get_link() instead */
304 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200305
306 /* The per-surface feedback flags */
307 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400308};
309
310static void
311weston_presentation_feedback_discard(
312 struct weston_presentation_feedback *feedback)
313{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200314 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400315 wl_resource_destroy(feedback->resource);
316}
317
318static void
319weston_presentation_feedback_discard_list(struct wl_list *list)
320{
321 struct weston_presentation_feedback *feedback, *tmp;
322
323 wl_list_for_each_safe(feedback, tmp, list, link)
324 weston_presentation_feedback_discard(feedback);
325}
326
327static void
328weston_presentation_feedback_present(
329 struct weston_presentation_feedback *feedback,
330 struct weston_output *output,
331 uint32_t refresh_nsec,
332 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200333 uint64_t seq,
334 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400335{
336 struct wl_client *client = wl_resource_get_client(feedback->resource);
337 struct wl_resource *o;
338 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400339
340 wl_resource_for_each(o, &output->resource_list) {
341 if (wl_resource_get_client(o) != client)
342 continue;
343
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200344 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400345 }
346
347 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200348 wp_presentation_feedback_send_presented(feedback->resource,
349 secs >> 32, secs & 0xffffffff,
350 ts->tv_nsec,
351 refresh_nsec,
352 seq >> 32, seq & 0xffffffff,
353 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400354 wl_resource_destroy(feedback->resource);
355}
356
357static void
358weston_presentation_feedback_present_list(struct wl_list *list,
359 struct weston_output *output,
360 uint32_t refresh_nsec,
361 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200362 uint64_t seq,
363 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400364{
365 struct weston_presentation_feedback *feedback, *tmp;
366
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200367 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200368 wl_list_empty(list));
369
Pekka Paalanen133e4392014-09-23 22:08:46 -0400370 wl_list_for_each_safe(feedback, tmp, list, link)
371 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200372 refresh_nsec, ts, seq,
373 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400374}
375
Jason Ekstrand7b982072014-05-20 14:33:03 -0500376static void
377surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
378{
379 struct weston_surface_state *state =
380 container_of(listener, struct weston_surface_state,
381 buffer_destroy_listener);
382
383 state->buffer = NULL;
384}
385
386static void
387weston_surface_state_init(struct weston_surface_state *state)
388{
389 state->newly_attached = 0;
390 state->buffer = NULL;
391 state->buffer_destroy_listener.notify =
392 surface_state_handle_buffer_destroy;
393 state->sx = 0;
394 state->sy = 0;
395
Derek Foreman152254b2015-11-26 14:17:48 -0600396 pixman_region32_init(&state->damage_surface);
397 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500398 pixman_region32_init(&state->opaque);
399 region_init_infinite(&state->input);
400
401 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400402 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500403
404 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
405 state->buffer_viewport.buffer.scale = 1;
406 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
407 state->buffer_viewport.surface.width = -1;
408 state->buffer_viewport.changed = 0;
409}
410
411static void
412weston_surface_state_fini(struct weston_surface_state *state)
413{
414 struct weston_frame_callback *cb, *next;
415
416 wl_list_for_each_safe(cb, next,
417 &state->frame_callback_list, link)
418 wl_resource_destroy(cb->resource);
419
Pekka Paalanen133e4392014-09-23 22:08:46 -0400420 weston_presentation_feedback_discard_list(&state->feedback_list);
421
Jason Ekstrand7b982072014-05-20 14:33:03 -0500422 pixman_region32_fini(&state->input);
423 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600424 pixman_region32_fini(&state->damage_surface);
425 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500426
427 if (state->buffer)
428 wl_list_remove(&state->buffer_destroy_listener.link);
429 state->buffer = NULL;
430}
431
432static void
433weston_surface_state_set_buffer(struct weston_surface_state *state,
434 struct weston_buffer *buffer)
435{
436 if (state->buffer == buffer)
437 return;
438
439 if (state->buffer)
440 wl_list_remove(&state->buffer_destroy_listener.link);
441 state->buffer = buffer;
442 if (state->buffer)
443 wl_signal_add(&state->buffer->destroy_signal,
444 &state->buffer_destroy_listener);
445}
446
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500447WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500448weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500449{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500450 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400451
Bryce Harringtonde16d892014-11-20 22:21:57 -0800452 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400453 if (surface == NULL)
454 return NULL;
455
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500456 wl_signal_init(&surface->destroy_signal);
457
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500458 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200459 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400460
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200461 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
462 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200463 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
464 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500465
466 weston_surface_state_init(&surface->pending);
467
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400468 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500469 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300470 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400471
Jason Ekstranda7af7042013-10-12 22:38:11 -0500472 wl_list_init(&surface->views);
473
474 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400475 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500476
Pekka Paalanene67858b2013-04-25 13:57:42 +0300477 wl_list_init(&surface->subsurface_list);
478 wl_list_init(&surface->subsurface_list_pending);
479
Jason Ekstrand1e059042014-10-16 10:55:19 -0500480 weston_matrix_init(&surface->buffer_to_surface_matrix);
481 weston_matrix_init(&surface->surface_to_buffer_matrix);
482
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400483 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500484}
485
Alex Wu8811bf92012-02-28 18:07:54 +0800486WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500487weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200488 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500489{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100490 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500491}
492
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400493WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500494weston_view_to_global_float(struct weston_view *view,
495 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200496{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500497 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200498 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
499
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200501
502 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200503 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700504 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200505 v.f[3]);
506 *x = 0;
507 *y = 0;
508 return;
509 }
510
511 *x = v.f[0] / v.f[3];
512 *y = v.f[1] / v.f[3];
513 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500514 *x = sx + view->geometry.x;
515 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200516 }
517}
518
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500519WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200520weston_transformed_coord(int width, int height,
521 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200522 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200523 float sx, float sy, float *bx, float *by)
524{
525 switch (transform) {
526 case WL_OUTPUT_TRANSFORM_NORMAL:
527 default:
528 *bx = sx;
529 *by = sy;
530 break;
531 case WL_OUTPUT_TRANSFORM_FLIPPED:
532 *bx = width - sx;
533 *by = sy;
534 break;
535 case WL_OUTPUT_TRANSFORM_90:
536 *bx = height - sy;
537 *by = sx;
538 break;
539 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
540 *bx = height - sy;
541 *by = width - sx;
542 break;
543 case WL_OUTPUT_TRANSFORM_180:
544 *bx = width - sx;
545 *by = height - sy;
546 break;
547 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
548 *bx = sx;
549 *by = height - sy;
550 break;
551 case WL_OUTPUT_TRANSFORM_270:
552 *bx = sy;
553 *by = width - sx;
554 break;
555 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
556 *bx = sy;
557 *by = sx;
558 break;
559 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200560
561 *bx *= scale;
562 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200563}
564
565WL_EXPORT pixman_box32_t
566weston_transformed_rect(int width, int height,
567 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200568 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200569 pixman_box32_t rect)
570{
571 float x1, x2, y1, y2;
572
573 pixman_box32_t ret;
574
Alexander Larsson4ea95522013-05-22 14:41:37 +0200575 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200576 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200577 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200578 rect.x2, rect.y2, &x2, &y2);
579
580 if (x1 <= x2) {
581 ret.x1 = x1;
582 ret.x2 = x2;
583 } else {
584 ret.x1 = x2;
585 ret.x2 = x1;
586 }
587
588 if (y1 <= y2) {
589 ret.y1 = y1;
590 ret.y2 = y2;
591 } else {
592 ret.y1 = y2;
593 ret.y2 = y1;
594 }
595
596 return ret;
597}
598
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600599/** Transform a region by a matrix, restricted to axis-aligned transformations
600 *
601 * Warning: This function does not work for projective, affine, or matrices
602 * that encode arbitrary rotations. Only 90-degree step rotations are
603 * supported.
604 */
605WL_EXPORT void
606weston_matrix_transform_region(pixman_region32_t *dest,
607 struct weston_matrix *matrix,
608 pixman_region32_t *src)
609{
610 pixman_box32_t *src_rects, *dest_rects;
611 int nrects, i;
612
613 src_rects = pixman_region32_rectangles(src, &nrects);
614 dest_rects = malloc(nrects * sizeof(*dest_rects));
615 if (!dest_rects)
616 return;
617
618 for (i = 0; i < nrects; i++) {
619 struct weston_vector vec1 = {{
620 src_rects[i].x1, src_rects[i].y1, 0, 1
621 }};
622 weston_matrix_transform(matrix, &vec1);
623 vec1.f[0] /= vec1.f[3];
624 vec1.f[1] /= vec1.f[3];
625
626 struct weston_vector vec2 = {{
627 src_rects[i].x2, src_rects[i].y2, 0, 1
628 }};
629 weston_matrix_transform(matrix, &vec2);
630 vec2.f[0] /= vec2.f[3];
631 vec2.f[1] /= vec2.f[3];
632
633 if (vec1.f[0] < vec2.f[0]) {
634 dest_rects[i].x1 = floor(vec1.f[0]);
635 dest_rects[i].x2 = ceil(vec2.f[0]);
636 } else {
637 dest_rects[i].x1 = floor(vec2.f[0]);
638 dest_rects[i].x2 = ceil(vec1.f[0]);
639 }
640
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600641 if (vec1.f[1] < vec2.f[1]) {
642 dest_rects[i].y1 = floor(vec1.f[1]);
643 dest_rects[i].y2 = ceil(vec2.f[1]);
644 } else {
645 dest_rects[i].y1 = floor(vec2.f[1]);
646 dest_rects[i].y2 = ceil(vec1.f[1]);
647 }
648 }
649
650 pixman_region32_clear(dest);
651 pixman_region32_init_rects(dest, dest_rects, nrects);
652 free(dest_rects);
653}
654
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200655WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500656weston_transformed_region(int width, int height,
657 enum wl_output_transform transform,
658 int32_t scale,
659 pixman_region32_t *src, pixman_region32_t *dest)
660{
661 pixman_box32_t *src_rects, *dest_rects;
662 int nrects, i;
663
664 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
665 if (src != dest)
666 pixman_region32_copy(dest, src);
667 return;
668 }
669
670 src_rects = pixman_region32_rectangles(src, &nrects);
671 dest_rects = malloc(nrects * sizeof(*dest_rects));
672 if (!dest_rects)
673 return;
674
675 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
676 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
677 } else {
678 for (i = 0; i < nrects; i++) {
679 switch (transform) {
680 default:
681 case WL_OUTPUT_TRANSFORM_NORMAL:
682 dest_rects[i].x1 = src_rects[i].x1;
683 dest_rects[i].y1 = src_rects[i].y1;
684 dest_rects[i].x2 = src_rects[i].x2;
685 dest_rects[i].y2 = src_rects[i].y2;
686 break;
687 case WL_OUTPUT_TRANSFORM_90:
688 dest_rects[i].x1 = height - src_rects[i].y2;
689 dest_rects[i].y1 = src_rects[i].x1;
690 dest_rects[i].x2 = height - src_rects[i].y1;
691 dest_rects[i].y2 = src_rects[i].x2;
692 break;
693 case WL_OUTPUT_TRANSFORM_180:
694 dest_rects[i].x1 = width - src_rects[i].x2;
695 dest_rects[i].y1 = height - src_rects[i].y2;
696 dest_rects[i].x2 = width - src_rects[i].x1;
697 dest_rects[i].y2 = height - src_rects[i].y1;
698 break;
699 case WL_OUTPUT_TRANSFORM_270:
700 dest_rects[i].x1 = src_rects[i].y1;
701 dest_rects[i].y1 = width - src_rects[i].x2;
702 dest_rects[i].x2 = src_rects[i].y2;
703 dest_rects[i].y2 = width - src_rects[i].x1;
704 break;
705 case WL_OUTPUT_TRANSFORM_FLIPPED:
706 dest_rects[i].x1 = width - src_rects[i].x2;
707 dest_rects[i].y1 = src_rects[i].y1;
708 dest_rects[i].x2 = width - src_rects[i].x1;
709 dest_rects[i].y2 = src_rects[i].y2;
710 break;
711 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
712 dest_rects[i].x1 = height - src_rects[i].y2;
713 dest_rects[i].y1 = width - src_rects[i].x2;
714 dest_rects[i].x2 = height - src_rects[i].y1;
715 dest_rects[i].y2 = width - src_rects[i].x1;
716 break;
717 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
718 dest_rects[i].x1 = src_rects[i].x1;
719 dest_rects[i].y1 = height - src_rects[i].y2;
720 dest_rects[i].x2 = src_rects[i].x2;
721 dest_rects[i].y2 = height - src_rects[i].y1;
722 break;
723 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
724 dest_rects[i].x1 = src_rects[i].y1;
725 dest_rects[i].y1 = src_rects[i].x1;
726 dest_rects[i].x2 = src_rects[i].y2;
727 dest_rects[i].y2 = src_rects[i].x2;
728 break;
729 }
730 }
731 }
732
733 if (scale != 1) {
734 for (i = 0; i < nrects; i++) {
735 dest_rects[i].x1 *= scale;
736 dest_rects[i].x2 *= scale;
737 dest_rects[i].y1 *= scale;
738 dest_rects[i].y2 *= scale;
739 }
740 }
741
742 pixman_region32_clear(dest);
743 pixman_region32_init_rects(dest, dest_rects, nrects);
744 free(dest_rects);
745}
746
Jonny Lamb74130762013-11-26 18:19:46 +0100747static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300748viewport_surface_to_buffer(struct weston_surface *surface,
749 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100750{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200751 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200752 double src_width, src_height;
753 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200754
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200755 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
756 if (vp->surface.width == -1) {
757 *bx = sx;
758 *by = sy;
759 return;
760 }
Jonny Lamb74130762013-11-26 18:19:46 +0100761
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200762 src_x = 0.0;
763 src_y = 0.0;
764 src_width = surface->width_from_buffer;
765 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100766 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200767 src_x = wl_fixed_to_double(vp->buffer.src_x);
768 src_y = wl_fixed_to_double(vp->buffer.src_y);
769 src_width = wl_fixed_to_double(vp->buffer.src_width);
770 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100771 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200772
773 *bx = sx * src_width / surface->width + src_x;
774 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100775}
776
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500777WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200778weston_surface_to_buffer_float(struct weston_surface *surface,
779 float sx, float sy, float *bx, float *by)
780{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200781 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
782
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300783 /* first transform coordinates if the viewport is set */
784 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100785
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500786 weston_transformed_coord(surface->width_from_buffer,
787 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200788 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100789 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200790}
791
Derek Foreman99739672015-12-03 16:38:11 -0600792/** Transform a rectangle from surface coordinates to buffer coordinates
793 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300794 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600795 * from.
796 * \param rect The rectangle to transform.
797 * \return The transformed rectangle.
798 *
799 * Viewport and buffer transformations can only do translation, scaling,
800 * and rotations in 90-degree steps. Therefore the only loss in the
801 * conversion is coordinate rounding.
802 *
803 * However, some coordinate rounding takes place as an intermediate
804 * step before the buffer scale factor is applied, so the rectangle
805 * boundary may not be exactly as expected.
806 *
807 * This is OK for damage tracking since a little extra coverage is
808 * not a problem.
809 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200810WL_EXPORT pixman_box32_t
811weston_surface_to_buffer_rect(struct weston_surface *surface,
812 pixman_box32_t rect)
813{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200814 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100815 float xf, yf;
816
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300817 /* first transform box coordinates if the viewport is set */
818 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100819 rect.x1 = floorf(xf);
820 rect.y1 = floorf(yf);
821
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300822 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600823 rect.x2 = ceilf(xf);
824 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100825
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500826 return weston_transformed_rect(surface->width_from_buffer,
827 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200828 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200829 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200830}
831
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200832/** Transform a region from surface coordinates to buffer coordinates
833 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300834 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200835 * from.
836 * \param surface_region[in] The region in surface coordinates.
837 * \param buffer_region[out] The region converted to buffer coordinates.
838 *
839 * Buffer_region must be init'd, but will be completely overwritten.
840 *
841 * Viewport and buffer transformations can only do translation, scaling,
842 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600843 * conversion is from the coordinate rounding that takes place in
844 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200845 */
846WL_EXPORT void
847weston_surface_to_buffer_region(struct weston_surface *surface,
848 pixman_region32_t *surface_region,
849 pixman_region32_t *buffer_region)
850{
851 pixman_box32_t *src_rects, *dest_rects;
852 int nrects, i;
853
854 src_rects = pixman_region32_rectangles(surface_region, &nrects);
855 dest_rects = malloc(nrects * sizeof(*dest_rects));
856 if (!dest_rects)
857 return;
858
859 for (i = 0; i < nrects; i++) {
860 dest_rects[i] = weston_surface_to_buffer_rect(surface,
861 src_rects[i]);
862 }
863
864 pixman_region32_fini(buffer_region);
865 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
866 free(dest_rects);
867}
868
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200869WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500870weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400871 struct weston_plane *plane)
872{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500873 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400874 return;
875
Jason Ekstranda7af7042013-10-12 22:38:11 -0500876 weston_view_damage_below(view);
877 view->plane = plane;
878 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400879}
880
Pekka Paalanen51723d52015-02-17 13:10:01 +0200881/** Inflict damage on the plane where the view is visible.
882 *
883 * \param view The view that causes the damage.
884 *
885 * If the view is currently on a plane (including the primary plane),
886 * take the view's boundingbox, subtract all the opaque views that cover it,
887 * and add the remaining region as damage to the plane. This corresponds
888 * to the damage inflicted to the plane if this view disappeared.
889 *
890 * A repaint is scheduled for this view.
891 *
892 * The region of all opaque views covering this view is stored in
893 * weston_view::clip and updated by view_accumulate_damage() during
894 * weston_output_repaint(). Specifically, that region matches the
895 * scenegraph as it was last painted.
896 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400897WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500898weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200899{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500900 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200901
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500902 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200903 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500904 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800905 if (view->plane)
906 pixman_region32_union(&view->plane->damage,
907 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500908 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800909 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200910}
911
Bryce Harrington3f650b82015-12-23 11:01:58 -0800912/**
913 * \param es The surface
914 * \param mask The new set of outputs for the surface
915 *
916 * Sets the surface's set of outputs to the ones specified by
917 * the new output mask provided. Identifies the outputs that
918 * have changed, the posts enter and leave events for these
919 * outputs as appropriate.
920 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400921static void
922weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
923{
924 uint32_t different = es->output_mask ^ mask;
925 uint32_t entered = mask & different;
926 uint32_t left = es->output_mask & different;
927 struct weston_output *output;
928 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500929 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400930
931 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500932 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400933 return;
934 if (different == 0)
935 return;
936
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500937 client = wl_resource_get_client(es->resource);
938
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400939 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800940 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400941 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500942 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400943 client);
944 if (resource == NULL)
945 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800946 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500947 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800948 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500949 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400950 }
951}
952
Bryce Harrington3f650b82015-12-23 11:01:58 -0800953/** Recalculate which output(s) the surface has views displayed on
954 *
955 * \param es The surface to remap to outputs
956 *
957 * Finds the output that is showing the largest amount of one
958 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300959 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800960 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300961 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800962 * in the output_mask for the surface.
963 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400964static void
965weston_surface_assign_output(struct weston_surface *es)
966{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 struct weston_output *new_output;
968 struct weston_view *view;
969 pixman_region32_t region;
970 uint32_t max, area, mask;
971 pixman_box32_t *e;
972
973 new_output = NULL;
974 max = 0;
975 mask = 0;
976 pixman_region32_init(&region);
977 wl_list_for_each(view, &es->views, surface_link) {
978 if (!view->output)
979 continue;
980
981 pixman_region32_intersect(&region, &view->transform.boundingbox,
982 &view->output->region);
983
984 e = pixman_region32_extents(&region);
985 area = (e->x2 - e->x1) * (e->y2 - e->y1);
986
987 mask |= view->output_mask;
988
989 if (area >= max) {
990 new_output = view->output;
991 max = area;
992 }
993 }
994 pixman_region32_fini(&region);
995
996 es->output = new_output;
997 weston_surface_update_output_mask(es, mask);
998}
999
Bryce Harrington3f650b82015-12-23 11:01:58 -08001000/** Recalculate which output(s) the view is displayed on
1001 *
1002 * \param ev The view to remap to outputs
1003 *
1004 * Identifies the set of outputs that the view is visible on,
1005 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001006 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001007 *
1008 * Also does the same for the view's surface. See
1009 * weston_surface_assign_output().
1010 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011static void
1012weston_view_assign_output(struct weston_view *ev)
1013{
1014 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001015 struct weston_output *output, *new_output;
1016 pixman_region32_t region;
1017 uint32_t max, area, mask;
1018 pixman_box32_t *e;
1019
1020 new_output = NULL;
1021 max = 0;
1022 mask = 0;
1023 pixman_region32_init(&region);
1024 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001025 if (output->destroying)
1026 continue;
1027
Jason Ekstranda7af7042013-10-12 22:38:11 -05001028 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001029 &output->region);
1030
1031 e = pixman_region32_extents(&region);
1032 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1033
1034 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001035 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001036
1037 if (area >= max) {
1038 new_output = output;
1039 max = area;
1040 }
1041 }
1042 pixman_region32_fini(&region);
1043
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044 ev->output = new_output;
1045 ev->output_mask = mask;
1046
1047 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001048}
1049
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001050static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001051weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1052 int from_x, int from_y, int *to_x, int *to_y)
1053{
1054 float x, y;
1055
1056 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1057 weston_view_from_global_float(to, x, y, &x, &y);
1058
1059 *to_x = round(x);
1060 *to_y = round(y);
1061}
1062
1063static void
1064weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1065{
1066 pixman_box32_t *a;
1067 pixman_box32_t b;
1068
1069 a = pixman_region32_extents(&from->geometry.scissor);
1070
1071 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1072 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1073
1074 pixman_region32_fini(&to->geometry.scissor);
1075 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1076}
1077
1078static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001079view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001081{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001082 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1083 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001084 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001085 { inbox->x1, inbox->y1 },
1086 { inbox->x1, inbox->y2 },
1087 { inbox->x2, inbox->y1 },
1088 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001089 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001090 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001091 int i;
1092
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001093 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001094 /* avoid rounding empty bbox to 1x1 */
1095 pixman_region32_init(bbox);
1096 return;
1097 }
1098
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001099 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001100 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001101 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001102 if (x < min_x)
1103 min_x = x;
1104 if (x > max_x)
1105 max_x = x;
1106 if (y < min_y)
1107 min_y = y;
1108 if (y > max_y)
1109 max_y = y;
1110 }
1111
Pekka Paalanen219b9822012-02-08 15:38:37 +02001112 int_x = floorf(min_x);
1113 int_y = floorf(min_y);
1114 pixman_region32_init_rect(bbox, int_x, int_y,
1115 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001116}
1117
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001118static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001120{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001122
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001123 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 view->geometry.x = roundf(view->geometry.x);
1125 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001126
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001127 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1129 view->transform.position.matrix.d[12] = view->geometry.x;
1130 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001131
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001133
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 view->transform.inverse = view->transform.position.matrix;
1135 view->transform.inverse.d[12] = -view->geometry.x;
1136 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001137
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001139 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001140 view->surface->width,
1141 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001142 if (view->geometry.scissor_enabled)
1143 pixman_region32_intersect(&view->transform.boundingbox,
1144 &view->transform.boundingbox,
1145 &view->geometry.scissor);
1146
1147 pixman_region32_translate(&view->transform.boundingbox,
1148 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001149
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 if (view->alpha == 1.0) {
1151 pixman_region32_copy(&view->transform.opaque,
1152 &view->surface->opaque);
1153 pixman_region32_translate(&view->transform.opaque,
1154 view->geometry.x,
1155 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001156 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001157}
1158
1159static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001161{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 struct weston_view *parent = view->geometry.parent;
1163 struct weston_matrix *matrix = &view->transform.matrix;
1164 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001165 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001166 pixman_region32_t surfregion;
1167 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001168
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001170
1171 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1173 view->transform.position.matrix.d[12] = view->geometry.x;
1174 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001175
1176 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001178 weston_matrix_multiply(matrix, &tform->matrix);
1179
Pekka Paalanen483243f2013-03-08 14:56:50 +02001180 if (parent)
1181 weston_matrix_multiply(matrix, &parent->transform.matrix);
1182
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001183 if (weston_matrix_invert(inverse, matrix) < 0) {
1184 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001185 weston_log("error: weston_view %p"
1186 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001187 return -1;
1188 }
1189
Pekka Paalanen380adf52015-02-16 14:39:11 +02001190 pixman_region32_init_rect(&surfregion, 0, 0,
1191 view->surface->width, view->surface->height);
1192 if (view->geometry.scissor_enabled)
1193 pixman_region32_intersect(&surfregion, &surfregion,
1194 &view->geometry.scissor);
1195 surfbox = pixman_region32_extents(&surfregion);
1196
1197 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1198 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001199
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001200 return 0;
1201}
1202
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001203static struct weston_layer *
1204get_view_layer(struct weston_view *view)
1205{
1206 if (view->parent_view)
1207 return get_view_layer(view->parent_view);
1208 return view->layer_link.layer;
1209}
1210
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001211WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001213{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001215 struct weston_layer *layer;
1216 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001217
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001219 return;
1220
Pekka Paalanen483243f2013-03-08 14:56:50 +02001221 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001223
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001225
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 pixman_region32_fini(&view->transform.boundingbox);
1229 pixman_region32_fini(&view->transform.opaque);
1230 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001231
Pekka Paalanencd403622012-01-25 13:37:39 +02001232 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 if (view->geometry.transformation_list.next ==
1234 &view->transform.position.link &&
1235 view->geometry.transformation_list.prev ==
1236 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001237 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001238 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001239 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 if (weston_view_update_transform_enable(view) < 0)
1241 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001242 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001243
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001244 layer = get_view_layer(view);
1245 if (layer) {
1246 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001247 pixman_region32_intersect(&view->transform.boundingbox,
1248 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001249 pixman_region32_intersect(&view->transform.opaque,
1250 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001251 pixman_region32_fini(&mask);
1252 }
1253
Pekka Paalanen380adf52015-02-16 14:39:11 +02001254 if (parent) {
1255 if (parent->geometry.scissor_enabled) {
1256 view->geometry.scissor_enabled = true;
1257 weston_view_transfer_scissor(parent, view);
1258 } else {
1259 view->geometry.scissor_enabled = false;
1260 }
1261 }
1262
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001266
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 wl_signal_emit(&view->surface->compositor->transform_signal,
1268 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001269}
1270
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001271WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001273{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001275
1276 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 * The invariant: if view->geometry.dirty, then all views
1278 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001279 * Corollary: if not parent->geometry.dirty, then all ancestors
1280 * are not dirty.
1281 */
1282
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001284 return;
1285
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001287
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001289 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001291}
1292
1293WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294weston_view_to_global_fixed(struct weston_view *view,
1295 wl_fixed_t vx, wl_fixed_t vy,
1296 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001297{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001298 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001299
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300 weston_view_to_global_float(view,
1301 wl_fixed_to_double(vx),
1302 wl_fixed_to_double(vy),
1303 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001304 *x = wl_fixed_from_double(xf);
1305 *y = wl_fixed_from_double(yf);
1306}
1307
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001308WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309weston_view_from_global_float(struct weston_view *view,
1310 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001311{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001313 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1314
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001316
1317 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001318 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001320 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 *vx = 0;
1322 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001323 return;
1324 }
1325
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 *vx = v.f[0] / v.f[3];
1327 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001328 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329 *vx = x - view->geometry.x;
1330 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001331 }
1332}
1333
1334WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335weston_view_from_global_fixed(struct weston_view *view,
1336 wl_fixed_t x, wl_fixed_t y,
1337 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001338{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001340
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 weston_view_from_global_float(view,
1342 wl_fixed_to_double(x),
1343 wl_fixed_to_double(y),
1344 &vxf, &vyf);
1345 *vx = wl_fixed_from_double(vxf);
1346 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001347}
1348
1349WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350weston_view_from_global(struct weston_view *view,
1351 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001352{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001354
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1356 *vx = floorf(vxf);
1357 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001358}
1359
Bryce Harrington3f650b82015-12-23 11:01:58 -08001360/**
1361 * \param surface The surface to be repainted
1362 *
1363 * Marks the output(s) that the surface is shown on as needing to be
1364 * repainted. See weston_output_schedule_repaint().
1365 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001366WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001367weston_surface_schedule_repaint(struct weston_surface *surface)
1368{
1369 struct weston_output *output;
1370
1371 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001372 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001373 weston_output_schedule_repaint(output);
1374}
1375
Bryce Harrington3f650b82015-12-23 11:01:58 -08001376/**
1377 * \param view The view to be repainted
1378 *
1379 * Marks the output(s) that the view is shown on as needing to be
1380 * repainted. See weston_output_schedule_repaint().
1381 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001382WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383weston_view_schedule_repaint(struct weston_view *view)
1384{
1385 struct weston_output *output;
1386
1387 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001388 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 weston_output_schedule_repaint(output);
1390}
1391
Pekka Paalanene508ce62015-02-19 13:59:55 +02001392/**
1393 * XXX: This function does it the wrong way.
1394 * surface->damage is the damage from the client, and causes
1395 * surface_flush_damage() to copy pixels. No window management action can
1396 * cause damage to the client-provided content, warranting re-upload!
1397 *
1398 * Instead of surface->damage, this function should record the damage
1399 * with all the views for this surface to avoid extraneous texture
1400 * uploads.
1401 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001403weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001404{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001405 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 0, 0, surface->width,
1407 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001408
Kristian Høgsberg98238702012-08-03 16:29:12 -04001409 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001410}
1411
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001412WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001414{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001415 if (view->geometry.x == x && view->geometry.y == y)
1416 return;
1417
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 view->geometry.x = x;
1419 view->geometry.y = y;
1420 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001421}
1422
Pekka Paalanen483243f2013-03-08 14:56:50 +02001423static void
1424transform_parent_handle_parent_destroy(struct wl_listener *listener,
1425 void *data)
1426{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001427 struct weston_view *view =
1428 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001429 geometry.parent_destroy_listener);
1430
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001432}
1433
1434WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001436 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001437{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 if (view->geometry.parent) {
1439 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1440 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001441
1442 if (!parent)
1443 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001444 }
1445
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001447
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001449 transform_parent_handle_parent_destroy;
1450 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001451 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001453 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001455 }
1456
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 weston_view_geometry_dirty(view);
1458}
1459
Pekka Paalanen380adf52015-02-16 14:39:11 +02001460/** Set a clip mask rectangle on a view
1461 *
1462 * \param view The view to set the clip mask on.
1463 * \param x Top-left corner X coordinate of the clip rectangle.
1464 * \param y Top-left corner Y coordinate of the clip rectangle.
1465 * \param width Width of the clip rectangle, non-negative.
1466 * \param height Height of the clip rectangle, non-negative.
1467 *
1468 * A shell may set a clip mask rectangle on a view. Everything outside
1469 * the rectangle is cut away for input and output purposes: it is
1470 * not drawn and cannot be hit by hit-test based input like pointer
1471 * motion or touch-downs. Everything inside the rectangle will behave
1472 * normally. Clients are unaware of clipping.
1473 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001474 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001475 * mask rectangle does not affect the view position, the view is positioned
1476 * as it would be without a clip. The clip also does not change
1477 * weston_surface::width,height.
1478 *
1479 * The clip mask rectangle is part of transformation inheritance
1480 * (weston_view_set_transform_parent()). A clip set in the root of the
1481 * transformation inheritance tree will affect all views in the tree.
1482 * A clip can be set only on the root view. Attempting to set a clip
1483 * on view that has a transformation parent will fail. Assigning a parent
1484 * to a view that has a clip set will cause the clip to be forgotten.
1485 *
1486 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1487 * on the additional transformations in the child views. These transformations
1488 * may not rotate the coordinate axes, i.e., only translation and scaling
1489 * are allowed. Violating this restriction causes the clipping to malfunction.
1490 * Furthermore, using scaling may cause rounding errors in child clipping.
1491 *
1492 * The clip mask rectangle is not automatically adjusted based on
1493 * wl_surface.attach dx and dy arguments.
1494 *
1495 * A clip mask rectangle can be set only if the compositor capability
1496 * WESTON_CAP_VIEW_CLIP_MASK is present.
1497 *
1498 * This function sets the clip mask rectangle and schedules a repaint for
1499 * the view.
1500 */
1501WL_EXPORT void
1502weston_view_set_mask(struct weston_view *view,
1503 int x, int y, int width, int height)
1504{
1505 struct weston_compositor *compositor = view->surface->compositor;
1506
1507 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1508 weston_log("%s not allowed without capability!\n", __func__);
1509 return;
1510 }
1511
1512 if (view->geometry.parent) {
1513 weston_log("view %p has a parent, clip forbidden!\n", view);
1514 return;
1515 }
1516
1517 if (width < 0 || height < 0) {
1518 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1519 x, y, width, height);
1520 return;
1521 }
1522
1523 pixman_region32_fini(&view->geometry.scissor);
1524 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1525 view->geometry.scissor_enabled = true;
1526 weston_view_geometry_dirty(view);
1527 weston_view_schedule_repaint(view);
1528}
1529
1530/** Remove the clip mask from a view
1531 *
1532 * \param view The view to remove the clip mask from.
1533 *
1534 * Removed the clip mask rectangle and schedules a repaint.
1535 *
1536 * \sa weston_view_set_mask
1537 */
1538WL_EXPORT void
1539weston_view_set_mask_infinite(struct weston_view *view)
1540{
1541 view->geometry.scissor_enabled = false;
1542 weston_view_geometry_dirty(view);
1543 weston_view_schedule_repaint(view);
1544}
1545
Derek Foreman280e7dd2014-10-03 13:13:42 -05001546WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547weston_view_is_mapped(struct weston_view *view)
1548{
1549 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001550 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001552 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001553}
1554
Derek Foreman280e7dd2014-10-03 13:13:42 -05001555WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001556weston_surface_is_mapped(struct weston_surface *surface)
1557{
1558 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001559 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001560 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001561 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001562}
1563
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001564static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001565surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001566{
1567 struct weston_view *view;
1568
1569 if (surface->width == width && surface->height == height)
1570 return;
1571
1572 surface->width = width;
1573 surface->height = height;
1574
1575 wl_list_for_each(view, &surface->views, surface_link)
1576 weston_view_geometry_dirty(view);
1577}
1578
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001579WL_EXPORT void
1580weston_surface_set_size(struct weston_surface *surface,
1581 int32_t width, int32_t height)
1582{
1583 assert(!surface->resource);
1584 surface_set_size(surface, width, height);
1585}
1586
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001587static int
1588fixed_round_up_to_int(wl_fixed_t f)
1589{
1590 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1591}
1592
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001593static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001594convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1595 int32_t width, int32_t height,
1596 uint32_t transform,
1597 int32_t scale)
1598{
1599 assert(scale > 0);
1600
1601 switch (transform) {
1602 case WL_OUTPUT_TRANSFORM_NORMAL:
1603 case WL_OUTPUT_TRANSFORM_180:
1604 case WL_OUTPUT_TRANSFORM_FLIPPED:
1605 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1606 *width_out = width / scale;
1607 *height_out = height / scale;
1608 break;
1609 case WL_OUTPUT_TRANSFORM_90:
1610 case WL_OUTPUT_TRANSFORM_270:
1611 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1612 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1613 *width_out = height / scale;
1614 *height_out = width / scale;
1615 break;
1616 default:
1617 assert(0 && "invalid transform");
1618 }
1619}
1620
1621static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001622weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001623{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001624 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001625
1626 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001627 surface->width_from_buffer = 0;
1628 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001629 return;
1630 }
1631
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001632 convert_size_by_transform_scale(&surface->width_from_buffer,
1633 &surface->height_from_buffer,
1634 surface->buffer_ref.buffer->width,
1635 surface->buffer_ref.buffer->height,
1636 vp->buffer.transform,
1637 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001638}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001639
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001640static void
1641weston_surface_update_size(struct weston_surface *surface)
1642{
1643 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1644 int32_t width, height;
1645
1646 width = surface->width_from_buffer;
1647 height = surface->height_from_buffer;
1648
1649 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001650 surface_set_size(surface,
1651 vp->surface.width, vp->surface.height);
1652 return;
1653 }
1654
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001655 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001656 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1657 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1658
1659 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001660 return;
1661 }
1662
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001663 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001664}
1665
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001666WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001667weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001668{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001669 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001670
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001671 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001672
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001673 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001674}
1675
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676WL_EXPORT struct weston_view *
1677weston_compositor_pick_view(struct weston_compositor *compositor,
1678 wl_fixed_t x, wl_fixed_t y,
1679 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001680{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001682 wl_fixed_t view_x, view_y;
1683 int view_ix, view_iy;
1684 int ix = wl_fixed_to_int(x);
1685 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001686
Jason Ekstranda7af7042013-10-12 22:38:11 -05001687 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001688 if (!pixman_region32_contains_point(
1689 &view->transform.boundingbox, ix, iy, NULL))
1690 continue;
1691
1692 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1693 view_ix = wl_fixed_to_int(view_x);
1694 view_iy = wl_fixed_to_int(view_y);
1695
1696 if (!pixman_region32_contains_point(&view->surface->input,
1697 view_ix, view_iy, NULL))
1698 continue;
1699
Pekka Paalanen380adf52015-02-16 14:39:11 +02001700 if (view->geometry.scissor_enabled &&
1701 !pixman_region32_contains_point(&view->geometry.scissor,
1702 view_ix, view_iy, NULL))
1703 continue;
1704
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001705 *vx = view_x;
1706 *vy = view_y;
1707 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001708 }
1709
Derek Foremanf9318d12015-05-11 15:40:11 -05001710 *vx = wl_fixed_from_int(-1000000);
1711 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001712 return NULL;
1713}
1714
1715static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001716weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001717{
Daniel Stone37816df2012-05-16 18:45:18 +01001718 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001719
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001720 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001721 return;
1722
Daniel Stone37816df2012-05-16 18:45:18 +01001723 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001724 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001725}
1726
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001727WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001728weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001729{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001730 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001731
Jason Ekstranda7af7042013-10-12 22:38:11 -05001732 if (!weston_view_is_mapped(view))
1733 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001734
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735 weston_view_damage_below(view);
1736 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001737 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001738 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739 wl_list_remove(&view->link);
1740 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 view->output_mask = 0;
1742 weston_surface_assign_output(view->surface);
1743
1744 if (weston_surface_is_mapped(view->surface))
1745 return;
1746
1747 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001748 struct weston_touch *touch = weston_seat_get_touch(seat);
1749 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1750 struct weston_keyboard *keyboard =
1751 weston_seat_get_keyboard(seat);
1752
1753 if (keyboard && keyboard->focus == view->surface)
1754 weston_keyboard_set_focus(keyboard, NULL);
1755 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001756 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001757 if (touch && touch->focus == view)
1758 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001759 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001760}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001761
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762WL_EXPORT void
1763weston_surface_unmap(struct weston_surface *surface)
1764{
1765 struct weston_view *view;
1766
1767 wl_list_for_each(view, &surface->views, surface_link)
1768 weston_view_unmap(view);
1769 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001770}
1771
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001772static void
1773weston_surface_reset_pending_buffer(struct weston_surface *surface)
1774{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001775 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001776 surface->pending.sx = 0;
1777 surface->pending.sy = 0;
1778 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001779 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001780}
1781
Jason Ekstranda7af7042013-10-12 22:38:11 -05001782WL_EXPORT void
1783weston_view_destroy(struct weston_view *view)
1784{
1785 wl_signal_emit(&view->destroy_signal, view);
1786
1787 assert(wl_list_empty(&view->geometry.child_list));
1788
1789 if (weston_view_is_mapped(view)) {
1790 weston_view_unmap(view);
1791 weston_compositor_build_view_list(view->surface->compositor);
1792 }
1793
1794 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001795 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796
1797 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001798 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001800 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801
1802 weston_view_set_transform_parent(view, NULL);
1803
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804 wl_list_remove(&view->surface_link);
1805
1806 free(view);
1807}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001808
1809WL_EXPORT void
1810weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001811{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001812 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001814
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001815 if (--surface->ref_count > 0)
1816 return;
1817
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001818 assert(surface->resource == NULL);
1819
Pekka Paalanenca790762015-04-17 14:23:38 +03001820 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001821
Pekka Paalanene67858b2013-04-25 13:57:42 +03001822 assert(wl_list_empty(&surface->subsurface_list_pending));
1823 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001824
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1826 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001827
Jason Ekstrand7b982072014-05-20 14:33:03 -05001828 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001829
Pekka Paalanende685b82012-12-04 15:58:12 +02001830 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001831
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001832 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001833 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001834 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001835
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001836 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001837 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001838
Pekka Paalanen133e4392014-09-23 22:08:46 -04001839 weston_presentation_feedback_discard_list(&surface->feedback_list);
1840
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001841 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001842}
1843
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001844static void
1845destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001846{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001847 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001848
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001849 assert(surface);
1850
Giulio Camuffo0d379742013-11-15 22:06:15 +01001851 /* Set the resource to NULL, since we don't want to leave a
1852 * dangling pointer if the surface was refcounted and survives
1853 * the weston_surface_destroy() call. */
1854 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001855
1856 if (surface->viewport_resource)
1857 wl_resource_set_user_data(surface->viewport_resource, NULL);
1858
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001859 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001860}
1861
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001862static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001863weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1864{
1865 struct weston_buffer *buffer =
1866 container_of(listener, struct weston_buffer, destroy_listener);
1867
1868 wl_signal_emit(&buffer->destroy_signal, buffer);
1869 free(buffer);
1870}
1871
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001872WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001873weston_buffer_from_resource(struct wl_resource *resource)
1874{
1875 struct weston_buffer *buffer;
1876 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001877
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001878 listener = wl_resource_get_destroy_listener(resource,
1879 weston_buffer_destroy_handler);
1880
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001881 if (listener)
1882 return container_of(listener, struct weston_buffer,
1883 destroy_listener);
1884
1885 buffer = zalloc(sizeof *buffer);
1886 if (buffer == NULL)
1887 return NULL;
1888
1889 buffer->resource = resource;
1890 wl_signal_init(&buffer->destroy_signal);
1891 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001892 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001893 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001894
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001895 return buffer;
1896}
1897
1898static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001899weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1900 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001901{
Pekka Paalanende685b82012-12-04 15:58:12 +02001902 struct weston_buffer_reference *ref =
1903 container_of(listener, struct weston_buffer_reference,
1904 destroy_listener);
1905
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001906 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001907 ref->buffer = NULL;
1908}
1909
1910WL_EXPORT void
1911weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001912 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001913{
1914 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001915 ref->buffer->busy_count--;
1916 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001917 assert(wl_resource_get_client(ref->buffer->resource));
1918 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001919 WL_BUFFER_RELEASE);
1920 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001921 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001922 }
1923
Pekka Paalanende685b82012-12-04 15:58:12 +02001924 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001925 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001926 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001927 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001928 }
1929
Pekka Paalanende685b82012-12-04 15:58:12 +02001930 ref->buffer = buffer;
1931 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1932}
1933
1934static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001935weston_surface_attach(struct weston_surface *surface,
1936 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001937{
1938 weston_buffer_reference(&surface->buffer_ref, buffer);
1939
Pekka Paalanena6421c42012-12-04 15:58:10 +02001940 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001941 if (weston_surface_is_mapped(surface))
1942 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001943 }
1944
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001945 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001946
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001947 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001948 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001949}
1950
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001951WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001952weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001953{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001954 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001955
1956 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001957 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001958}
1959
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001960WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001961weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001962{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001963 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001964
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001965 pixman_region32_union(&compositor->primary_plane.damage,
1966 &compositor->primary_plane.damage,
1967 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001968 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001969}
1970
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001971static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001972surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001973{
Pekka Paalanende685b82012-12-04 15:58:12 +02001974 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001975 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001976 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001977
Pekka Paalanenb5026542014-11-12 15:09:24 +02001978 if (weston_timeline_enabled_ &&
1979 pixman_region32_not_empty(&surface->damage))
1980 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1981 TLP_OUTPUT(surface->output), TLP_END);
1982
Jason Ekstrandef540082014-06-26 10:37:36 -07001983 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001984}
1985
1986static void
1987view_accumulate_damage(struct weston_view *view,
1988 pixman_region32_t *opaque)
1989{
1990 pixman_region32_t damage;
1991
1992 pixman_region32_init(&damage);
1993 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001994 pixman_box32_t *extents;
1995
Jason Ekstranda7af7042013-10-12 22:38:11 -05001996 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001997 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001998 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001999 pixman_region32_copy(&damage, &view->surface->damage);
2000 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002001 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002002 }
2003
Pekka Paalanen380adf52015-02-16 14:39:11 +02002004 pixman_region32_intersect(&damage, &damage,
2005 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002006 pixman_region32_subtract(&damage, &damage, opaque);
2007 pixman_region32_union(&view->plane->damage,
2008 &view->plane->damage, &damage);
2009 pixman_region32_fini(&damage);
2010 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002011 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002012}
2013
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002014static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002015compositor_accumulate_damage(struct weston_compositor *ec)
2016{
2017 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002019 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002020
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002021 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002022
2023 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002024 pixman_region32_copy(&plane->clip, &clip);
2025
2026 pixman_region32_init(&opaque);
2027
Jason Ekstranda7af7042013-10-12 22:38:11 -05002028 wl_list_for_each(ev, &ec->view_list, link) {
2029 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002030 continue;
2031
Jason Ekstranda7af7042013-10-12 22:38:11 -05002032 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002033 }
2034
2035 pixman_region32_union(&clip, &clip, &opaque);
2036 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002037 }
2038
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002039 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002040
Jason Ekstranda7af7042013-10-12 22:38:11 -05002041 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002042 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002043
2044 wl_list_for_each(ev, &ec->view_list, link) {
2045 if (ev->surface->touched)
2046 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002047 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002048
2049 surface_flush_damage(ev->surface);
2050
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002051 /* Both the renderer and the backend have seen the buffer
2052 * by now. If renderer needs the buffer, it has its own
2053 * reference set. If the backend wants to keep the buffer
2054 * around for migrating the surface into a non-primary plane
2055 * later, keep_buffer is true. Otherwise, drop the core
2056 * reference now, and allow early buffer release. This enables
2057 * clients to use single-buffering.
2058 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002059 if (!ev->surface->keep_buffer)
2060 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002061 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002062}
2063
2064static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002065surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002066{
2067 struct weston_subsurface *sub;
2068
Pekka Paalanene67858b2013-04-25 13:57:42 +03002069 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002071 continue;
2072
Jason Ekstranda7af7042013-10-12 22:38:11 -05002073 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2074 wl_list_init(&sub->surface->views);
2075
2076 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002077 }
2078}
2079
2080static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002081surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002082{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 struct weston_subsurface *sub;
2084 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002085
Jason Ekstranda7af7042013-10-12 22:38:11 -05002086 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2087 if (sub->surface == surface)
2088 continue;
2089
George Kiagiadakised04d382014-06-13 18:10:26 +02002090 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2091 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002093 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002094
2095 surface_free_unused_subsurface_views(sub->surface);
2096 }
2097}
2098
2099static void
2100view_list_add_subsurface_view(struct weston_compositor *compositor,
2101 struct weston_subsurface *sub,
2102 struct weston_view *parent)
2103{
2104 struct weston_subsurface *child;
2105 struct weston_view *view = NULL, *iv;
2106
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002107 if (!weston_surface_is_mapped(sub->surface))
2108 return;
2109
Jason Ekstranda7af7042013-10-12 22:38:11 -05002110 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2111 if (iv->geometry.parent == parent) {
2112 view = iv;
2113 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002114 }
2115 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116
2117 if (view) {
2118 /* Put it back in the surface's list of views */
2119 wl_list_remove(&view->surface_link);
2120 wl_list_insert(&sub->surface->views, &view->surface_link);
2121 } else {
2122 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002123 weston_view_set_position(view,
2124 sub->position.x,
2125 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002126 weston_view_set_transform_parent(view, parent);
2127 }
2128
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002129 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002130 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131
Pekka Paalanenb188e912013-11-19 14:03:35 +02002132 if (wl_list_empty(&sub->surface->subsurface_list)) {
2133 wl_list_insert(compositor->view_list.prev, &view->link);
2134 return;
2135 }
2136
2137 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2138 if (child->surface == sub->surface)
2139 wl_list_insert(compositor->view_list.prev, &view->link);
2140 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002142 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143}
2144
2145static void
2146view_list_add(struct weston_compositor *compositor,
2147 struct weston_view *view)
2148{
2149 struct weston_subsurface *sub;
2150
2151 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002152
Pekka Paalanenb188e912013-11-19 14:03:35 +02002153 if (wl_list_empty(&view->surface->subsurface_list)) {
2154 wl_list_insert(compositor->view_list.prev, &view->link);
2155 return;
2156 }
2157
2158 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2159 if (sub->surface == view->surface)
2160 wl_list_insert(compositor->view_list.prev, &view->link);
2161 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002162 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002163 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002164}
2165
2166static void
2167weston_compositor_build_view_list(struct weston_compositor *compositor)
2168{
2169 struct weston_view *view;
2170 struct weston_layer *layer;
2171
2172 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002173 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002174 surface_stash_subsurface_views(view->surface);
2175
2176 wl_list_init(&compositor->view_list);
2177 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002178 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002179 view_list_add(compositor, view);
2180 }
2181 }
2182
2183 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002184 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002185 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002186}
2187
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002188static void
2189weston_output_take_feedback_list(struct weston_output *output,
2190 struct weston_surface *surface)
2191{
2192 struct weston_view *view;
2193 struct weston_presentation_feedback *feedback;
2194 uint32_t flags = 0xffffffff;
2195
2196 if (wl_list_empty(&surface->feedback_list))
2197 return;
2198
2199 /* All views must have the flag for the flag to survive. */
2200 wl_list_for_each(view, &surface->views, surface_link) {
2201 /* ignore views that are not on this output at all */
2202 if (view->output_mask & (1u << output->id))
2203 flags &= view->psf_flags;
2204 }
2205
2206 wl_list_for_each(feedback, &surface->feedback_list, link)
2207 feedback->psf_flags = flags;
2208
2209 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2210 wl_list_init(&surface->feedback_list);
2211}
2212
David Herrmann1edf44c2013-10-22 17:11:26 +02002213static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002214weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002215{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002216 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002217 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002218 struct weston_animation *animation, *next;
2219 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002220 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002221 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002222 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002223
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002224 if (output->destroying)
2225 return 0;
2226
Pekka Paalanenb5026542014-11-12 15:09:24 +02002227 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2228
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002229 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002231
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002232 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002233 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002234 } else {
2235 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002236 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002237 ev->psf_flags = 0;
2238 }
2239 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002240
Pekka Paalanene67858b2013-04-25 13:57:42 +03002241 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002242 wl_list_for_each(ev, &ec->view_list, link) {
2243 /* Note: This operation is safe to do multiple times on the
2244 * same surface.
2245 */
2246 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002247 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002248 &ev->surface->frame_callback_list);
2249 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002250
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002251 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002252 }
2253 }
2254
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002255 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002256
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002257 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002258 pixman_region32_intersect(&output_damage,
2259 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002260 pixman_region32_subtract(&output_damage,
2261 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002262
Scott Moreauccbf29d2012-02-22 14:21:41 -07002263 if (output->dirty)
2264 weston_output_update_matrix(output);
2265
David Herrmann1edf44c2013-10-22 17:11:26 +02002266 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002267
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002268 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002269
Kristian Høgsbergef044142011-06-21 15:02:12 -04002270 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002271
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002272 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002273
Jonas Ådahldb773762012-06-13 00:01:21 +02002274 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002275 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002276 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002277 }
2278
Scott Moreaud64cf212012-06-08 19:40:54 -06002279 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002280 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002281 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002282 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002283
Pekka Paalanenb5026542014-11-12 15:09:24 +02002284 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2285
David Herrmann1edf44c2013-10-22 17:11:26 +02002286 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002287}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002288
Pekka Paalanen82919792014-05-21 13:51:49 +03002289static void
2290weston_output_schedule_repaint_reset(struct weston_output *output)
2291{
Pekka Paalanen82919792014-05-21 13:51:49 +03002292 output->repaint_scheduled = 0;
2293 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002294}
2295
Pekka Paalanen0513a952014-05-21 16:17:27 +03002296static int
2297output_repaint_timer_handler(void *data)
2298{
2299 struct weston_output *output = data;
2300 struct weston_compositor *compositor = output->compositor;
2301
2302 if (output->repaint_needed &&
2303 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2304 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2305 weston_output_repaint(output) == 0)
2306 return 0;
2307
2308 weston_output_schedule_repaint_reset(output);
2309
2310 return 0;
2311}
2312
Kristian Høgsbergef044142011-06-21 15:02:12 -04002313WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002314weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002315 const struct timespec *stamp,
2316 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002317{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002318 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002319 int32_t refresh_nsec;
2320 struct timespec now;
2321 struct timespec gone;
2322 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002323
Pekka Paalanenb5026542014-11-12 15:09:24 +02002324 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2325 TLP_VBLANK(stamp), TLP_END);
2326
Pekka Paalanend7894d02015-07-03 15:08:53 +03002327 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002328 weston_presentation_feedback_present_list(&output->feedback_list,
2329 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002330 output->msc,
2331 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002332
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002333 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002334
Pekka Paalanen0513a952014-05-21 16:17:27 +03002335 weston_compositor_read_presentation_clock(compositor, &now);
2336 timespec_sub(&gone, &now, stamp);
2337 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2338 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002339
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002340 if (msec < -1000 || msec > 1000) {
2341 static bool warned;
2342
2343 if (!warned)
2344 weston_log("Warning: computed repaint delay is "
2345 "insane: %d msec\n", msec);
2346 warned = true;
2347
2348 msec = 0;
2349 }
2350
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002351 /* Called from restart_repaint_loop and restart happens already after
2352 * the deadline given by repaint_msec? In that case we delay until
2353 * the deadline of the next frame, to give clients a more predictable
2354 * timing of the repaint cycle to lock on. */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02002355 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID && msec < 0)
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002356 msec += refresh_nsec / 1000000;
2357
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002358 if (msec < 1)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002359 output_repaint_timer_handler(output);
2360 else
2361 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002362}
2363
2364static void
2365idle_repaint(void *data)
2366{
2367 struct weston_output *output = data;
2368
Jonas Ådahle5a12252013-04-05 23:07:11 +02002369 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002370}
2371
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002372WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002373weston_layer_entry_insert(struct weston_layer_entry *list,
2374 struct weston_layer_entry *entry)
2375{
2376 wl_list_insert(&list->link, &entry->link);
2377 entry->layer = list->layer;
2378}
2379
2380WL_EXPORT void
2381weston_layer_entry_remove(struct weston_layer_entry *entry)
2382{
2383 wl_list_remove(&entry->link);
2384 wl_list_init(&entry->link);
2385 entry->layer = NULL;
2386}
2387
2388WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002389weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2390{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002391 wl_list_init(&layer->view_list.link);
2392 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002393 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002394 if (below != NULL)
2395 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002396}
2397
2398WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002399weston_layer_set_mask(struct weston_layer *layer,
2400 int x, int y, int width, int height)
2401{
2402 struct weston_view *view;
2403
2404 layer->mask.x1 = x;
2405 layer->mask.x2 = x + width;
2406 layer->mask.y1 = y;
2407 layer->mask.y2 = y + height;
2408
2409 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2410 weston_view_geometry_dirty(view);
2411 }
2412}
2413
2414WL_EXPORT void
2415weston_layer_set_mask_infinite(struct weston_layer *layer)
2416{
2417 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2418 UINT32_MAX, UINT32_MAX);
2419}
2420
2421WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002422weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002423{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002424 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002425 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002426
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002427 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2428 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002429 return;
2430
Pekka Paalanenb5026542014-11-12 15:09:24 +02002431 if (!output->repaint_needed)
2432 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2433
Kristian Høgsbergef044142011-06-21 15:02:12 -04002434 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002435 output->repaint_needed = 1;
2436 if (output->repaint_scheduled)
2437 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002438
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002439 wl_event_loop_add_idle(loop, idle_repaint, output);
2440 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002441 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002442}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002443
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002444WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002445weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2446{
2447 struct weston_output *output;
2448
2449 wl_list_for_each(output, &compositor->output_list, link)
2450 weston_output_schedule_repaint(output);
2451}
2452
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002453static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002454surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002455{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002456 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002457}
2458
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002459static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002460surface_attach(struct wl_client *client,
2461 struct wl_resource *resource,
2462 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2463{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002464 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002465 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002466
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002467 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002468 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002469 if (buffer == NULL) {
2470 wl_client_post_no_memory(client);
2471 return;
2472 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002473 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002474
Pekka Paalanende685b82012-12-04 15:58:12 +02002475 /* Attach, attach, without commit in between does not send
2476 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002477 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002478
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002479 surface->pending.sx = sx;
2480 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002481 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002482}
2483
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002484static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002485surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002486 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002487 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002488{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002489 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002490
Derek Foreman57e92ed2015-11-17 14:11:35 -06002491 if (width <= 0 || height <= 0)
2492 return;
2493
Derek Foreman152254b2015-11-26 14:17:48 -06002494 pixman_region32_union_rect(&surface->pending.damage_surface,
2495 &surface->pending.damage_surface,
2496 x, y, width, height);
2497}
2498
2499static void
2500surface_damage_buffer(struct wl_client *client,
2501 struct wl_resource *resource,
2502 int32_t x, int32_t y, int32_t width, int32_t height)
2503{
2504 struct weston_surface *surface = wl_resource_get_user_data(resource);
2505
2506 if (width <= 0 || height <= 0)
2507 return;
2508
2509 pixman_region32_union_rect(&surface->pending.damage_buffer,
2510 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002511 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002512}
2513
Kristian Høgsberg33418202011-08-16 23:01:28 -04002514static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002515destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002516{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002517 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002518
2519 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002520 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002521}
2522
2523static void
2524surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002525 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002526{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002527 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002528 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002529
2530 cb = malloc(sizeof *cb);
2531 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002532 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002533 return;
2534 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002535
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002536 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2537 callback);
2538 if (cb->resource == NULL) {
2539 free(cb);
2540 wl_resource_post_no_memory(resource);
2541 return;
2542 }
2543
Jason Ekstranda85118c2013-06-27 20:17:02 -05002544 wl_resource_set_implementation(cb->resource, NULL, cb,
2545 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002546
Pekka Paalanenbc106382012-10-10 12:49:31 +03002547 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002548}
2549
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002550static void
2551surface_set_opaque_region(struct wl_client *client,
2552 struct wl_resource *resource,
2553 struct wl_resource *region_resource)
2554{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002555 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002556 struct weston_region *region;
2557
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002558 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002559 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002560 pixman_region32_copy(&surface->pending.opaque,
2561 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002562 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002563 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002564 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002565}
2566
2567static void
2568surface_set_input_region(struct wl_client *client,
2569 struct wl_resource *resource,
2570 struct wl_resource *region_resource)
2571{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002572 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002573 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002574
2575 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002576 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002577 pixman_region32_copy(&surface->pending.input,
2578 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002579 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002580 pixman_region32_fini(&surface->pending.input);
2581 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002582 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002583}
2584
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002585static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002586weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002587{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002588 struct weston_subsurface *sub;
2589
2590 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2591 parent_link_pending) {
2592 wl_list_remove(&sub->parent_link);
2593 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2594 }
2595}
2596
2597static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002598weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002599 struct weston_matrix *matrix)
2600{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002601 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002602 double src_width, src_height, dest_width, dest_height;
2603
2604 weston_matrix_init(matrix);
2605
2606 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2607 src_width = surface->width_from_buffer;
2608 src_height = surface->height_from_buffer;
2609 } else {
2610 src_width = wl_fixed_to_double(vp->buffer.src_width);
2611 src_height = wl_fixed_to_double(vp->buffer.src_height);
2612 }
2613
2614 if (vp->surface.width == -1) {
2615 dest_width = src_width;
2616 dest_height = src_height;
2617 } else {
2618 dest_width = vp->surface.width;
2619 dest_height = vp->surface.height;
2620 }
2621
2622 if (src_width != dest_width || src_height != dest_height)
2623 weston_matrix_scale(matrix,
2624 src_width / dest_width,
2625 src_height / dest_height, 1);
2626
2627 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2628 weston_matrix_translate(matrix,
2629 wl_fixed_to_double(vp->buffer.src_x),
2630 wl_fixed_to_double(vp->buffer.src_y),
2631 0);
2632
2633 switch (vp->buffer.transform) {
2634 case WL_OUTPUT_TRANSFORM_FLIPPED:
2635 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2636 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2637 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2638 weston_matrix_scale(matrix, -1, 1, 1);
2639 weston_matrix_translate(matrix,
2640 surface->width_from_buffer, 0, 0);
2641 break;
2642 }
2643
2644 switch (vp->buffer.transform) {
2645 default:
2646 case WL_OUTPUT_TRANSFORM_NORMAL:
2647 case WL_OUTPUT_TRANSFORM_FLIPPED:
2648 break;
2649 case WL_OUTPUT_TRANSFORM_90:
2650 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2651 weston_matrix_rotate_xy(matrix, 0, 1);
2652 weston_matrix_translate(matrix,
2653 surface->height_from_buffer, 0, 0);
2654 break;
2655 case WL_OUTPUT_TRANSFORM_180:
2656 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2657 weston_matrix_rotate_xy(matrix, -1, 0);
2658 weston_matrix_translate(matrix,
2659 surface->width_from_buffer,
2660 surface->height_from_buffer, 0);
2661 break;
2662 case WL_OUTPUT_TRANSFORM_270:
2663 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2664 weston_matrix_rotate_xy(matrix, 0, -1);
2665 weston_matrix_translate(matrix,
2666 0, surface->width_from_buffer, 0);
2667 break;
2668 }
2669
2670 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2671}
2672
Derek Foreman152254b2015-11-26 14:17:48 -06002673/* Translate pending damage in buffer co-ordinates to surface
2674 * co-ordinates and union it with a pixman_region32_t.
2675 * This should only be called after the buffer is attached.
2676 */
2677static void
2678apply_damage_buffer(pixman_region32_t *dest,
2679 struct weston_surface *surface,
2680 struct weston_surface_state *state)
2681{
2682 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2683
2684 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2685 * translated into surface co-ordinates and unioned with
2686 * any other surface damage.
2687 * None of this makes sense if there is no buffer though.
2688 */
2689 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2690 pixman_region32_t buffer_damage;
2691
2692 pixman_region32_intersect_rect(&state->damage_buffer,
2693 &state->damage_buffer,
2694 0, 0, buffer->width,
2695 buffer->height);
2696 pixman_region32_init(&buffer_damage);
2697 weston_matrix_transform_region(&buffer_damage,
2698 &surface->buffer_to_surface_matrix,
2699 &state->damage_buffer);
2700 pixman_region32_union(dest, dest, &buffer_damage);
2701 pixman_region32_fini(&buffer_damage);
2702 }
2703 /* We should clear this on commit even if there was no buffer */
2704 pixman_region32_clear(&state->damage_buffer);
2705}
2706
Jason Ekstrand1e059042014-10-16 10:55:19 -05002707static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002708weston_surface_commit_state(struct weston_surface *surface,
2709 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002710{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002711 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002712 pixman_region32_t opaque;
2713
Alexander Larsson4ea95522013-05-22 14:41:37 +02002714 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002715 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002716 /* wp_viewport.set_source */
2717 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002718 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002719
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002720 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002721 if (state->newly_attached)
2722 weston_surface_attach(surface, state->buffer);
2723 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002724
Jason Ekstrand1e059042014-10-16 10:55:19 -05002725 weston_surface_build_buffer_matrix(surface,
2726 &surface->surface_to_buffer_matrix);
2727 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2728 &surface->surface_to_buffer_matrix);
2729
Jason Ekstrand7b982072014-05-20 14:33:03 -05002730 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002731 weston_surface_update_size(surface);
2732 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002733 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002734 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002735
Jason Ekstrand7b982072014-05-20 14:33:03 -05002736 state->sx = 0;
2737 state->sy = 0;
2738 state->newly_attached = 0;
2739 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002740
Derek Foreman152254b2015-11-26 14:17:48 -06002741 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002742 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002743 (pixman_region32_not_empty(&state->damage_surface) ||
2744 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002745 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002746
Pekka Paalanen8e159182012-10-10 12:49:25 +03002747 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002748 &state->damage_surface);
2749
2750 apply_damage_buffer(&surface->damage, surface, state);
2751
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002752 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002753 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002754 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002755
2756 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002757 pixman_region32_init(&opaque);
2758 pixman_region32_intersect_rect(&opaque, &state->opaque,
2759 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002760
2761 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2762 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763 wl_list_for_each(view, &surface->views, surface_link)
2764 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002765 }
2766
2767 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002768
2769 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002770 pixman_region32_intersect_rect(&surface->input, &state->input,
2771 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002772
Pekka Paalanenbc106382012-10-10 12:49:31 +03002773 /* wl_surface.frame */
2774 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002775 &state->frame_callback_list);
2776 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002777
2778 /* XXX:
2779 * What should happen with a feedback request, if there
2780 * is no wl_buffer attached for this commit?
2781 */
2782
2783 /* presentation.feedback */
2784 wl_list_insert_list(&surface->feedback_list,
2785 &state->feedback_list);
2786 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002787}
2788
2789static void
2790weston_surface_commit(struct weston_surface *surface)
2791{
2792 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002793
Pekka Paalanene67858b2013-04-25 13:57:42 +03002794 weston_surface_commit_subsurface_order(surface);
2795
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002796 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002797}
2798
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002799static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002800weston_subsurface_commit(struct weston_subsurface *sub);
2801
2802static void
2803weston_subsurface_parent_commit(struct weston_subsurface *sub,
2804 int parent_is_synchronized);
2805
2806static void
2807surface_commit(struct wl_client *client, struct wl_resource *resource)
2808{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002809 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002810 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2811
2812 if (sub) {
2813 weston_subsurface_commit(sub);
2814 return;
2815 }
2816
2817 weston_surface_commit(surface);
2818
2819 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2820 if (sub->surface != surface)
2821 weston_subsurface_parent_commit(sub, 0);
2822 }
2823}
2824
2825static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002826surface_set_buffer_transform(struct wl_client *client,
2827 struct wl_resource *resource, int transform)
2828{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002829 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002830
Jonny Lamba55f1392014-05-30 12:07:15 +02002831 /* if wl_output.transform grows more members this will need to be updated. */
2832 if (transform < 0 ||
2833 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2834 wl_resource_post_error(resource,
2835 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2836 "buffer transform must be a valid transform "
2837 "('%d' specified)", transform);
2838 return;
2839 }
2840
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002841 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002842 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002843}
2844
Alexander Larsson4ea95522013-05-22 14:41:37 +02002845static void
2846surface_set_buffer_scale(struct wl_client *client,
2847 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002848 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002849{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002850 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002851
Jonny Lamba55f1392014-05-30 12:07:15 +02002852 if (scale < 1) {
2853 wl_resource_post_error(resource,
2854 WL_SURFACE_ERROR_INVALID_SCALE,
2855 "buffer scale must be at least one "
2856 "('%d' specified)", scale);
2857 return;
2858 }
2859
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002860 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002861 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002862}
2863
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002864static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002865 surface_destroy,
2866 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002867 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002868 surface_frame,
2869 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002870 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002871 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002872 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06002873 surface_set_buffer_scale,
2874 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002875};
2876
2877static void
2878compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002879 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002880{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002881 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002882 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002883
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002884 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002885 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002886 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002887 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002888 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002889
Jason Ekstranda85118c2013-06-27 20:17:02 -05002890 surface->resource =
2891 wl_resource_create(client, &wl_surface_interface,
2892 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002893 if (surface->resource == NULL) {
2894 weston_surface_destroy(surface);
2895 wl_resource_post_no_memory(resource);
2896 return;
2897 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002898 wl_resource_set_implementation(surface->resource, &surface_interface,
2899 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002900
2901 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002902}
2903
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002904static void
2905destroy_region(struct wl_resource *resource)
2906{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002907 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002908
2909 pixman_region32_fini(&region->region);
2910 free(region);
2911}
2912
2913static void
2914region_destroy(struct wl_client *client, struct wl_resource *resource)
2915{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002916 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002917}
2918
2919static void
2920region_add(struct wl_client *client, struct wl_resource *resource,
2921 int32_t x, int32_t y, int32_t width, int32_t height)
2922{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002923 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002924
2925 pixman_region32_union_rect(&region->region, &region->region,
2926 x, y, width, height);
2927}
2928
2929static void
2930region_subtract(struct wl_client *client, struct wl_resource *resource,
2931 int32_t x, int32_t y, int32_t width, int32_t height)
2932{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002933 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002934 pixman_region32_t rect;
2935
2936 pixman_region32_init_rect(&rect, x, y, width, height);
2937 pixman_region32_subtract(&region->region, &region->region, &rect);
2938 pixman_region32_fini(&rect);
2939}
2940
2941static const struct wl_region_interface region_interface = {
2942 region_destroy,
2943 region_add,
2944 region_subtract
2945};
2946
2947static void
2948compositor_create_region(struct wl_client *client,
2949 struct wl_resource *resource, uint32_t id)
2950{
2951 struct weston_region *region;
2952
2953 region = malloc(sizeof *region);
2954 if (region == NULL) {
2955 wl_resource_post_no_memory(resource);
2956 return;
2957 }
2958
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002959 pixman_region32_init(&region->region);
2960
Jason Ekstranda85118c2013-06-27 20:17:02 -05002961 region->resource =
2962 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002963 if (region->resource == NULL) {
2964 free(region);
2965 wl_resource_post_no_memory(resource);
2966 return;
2967 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002968 wl_resource_set_implementation(region->resource, &region_interface,
2969 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002970}
2971
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002972static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002973 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002974 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002975};
2976
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002977static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002978weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2979{
2980 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002981
Jason Ekstrand7b982072014-05-20 14:33:03 -05002982 weston_surface_commit_state(surface, &sub->cached);
2983 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002984
2985 weston_surface_commit_subsurface_order(surface);
2986
2987 weston_surface_schedule_repaint(surface);
2988
Jason Ekstrand7b982072014-05-20 14:33:03 -05002989 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002990}
2991
2992static void
2993weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2994{
2995 struct weston_surface *surface = sub->surface;
2996
2997 /*
2998 * If this commit would cause the surface to move by the
2999 * attach(dx, dy) parameters, the old damage region must be
3000 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003001 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003002 */
Derek Foreman152254b2015-11-26 14:17:48 -06003003 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003004 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003005 pixman_region32_union(&sub->cached.damage_surface,
3006 &sub->cached.damage_surface,
3007 &surface->pending.damage_surface);
3008 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003009
3010 if (surface->pending.newly_attached) {
3011 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003012 weston_surface_state_set_buffer(&sub->cached,
3013 surface->pending.buffer);
3014 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003015 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003016 weston_presentation_feedback_discard_list(
3017 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003018 }
3019 sub->cached.sx += surface->pending.sx;
3020 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003021
Derek Foreman152254b2015-11-26 14:17:48 -06003022 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3023
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003024 sub->cached.buffer_viewport.changed |=
3025 surface->pending.buffer_viewport.changed;
3026 sub->cached.buffer_viewport.buffer =
3027 surface->pending.buffer_viewport.buffer;
3028 sub->cached.buffer_viewport.surface =
3029 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003030
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003031 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003032
3033 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3034
3035 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3036
3037 wl_list_insert_list(&sub->cached.frame_callback_list,
3038 &surface->pending.frame_callback_list);
3039 wl_list_init(&surface->pending.frame_callback_list);
3040
Pekka Paalanen133e4392014-09-23 22:08:46 -04003041 wl_list_insert_list(&sub->cached.feedback_list,
3042 &surface->pending.feedback_list);
3043 wl_list_init(&surface->pending.feedback_list);
3044
Jason Ekstrand7b982072014-05-20 14:33:03 -05003045 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003046}
3047
Derek Foreman280e7dd2014-10-03 13:13:42 -05003048static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003049weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3050{
3051 while (sub) {
3052 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003053 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003054
3055 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003056 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003057
3058 sub = weston_surface_to_subsurface(sub->parent);
3059 }
3060
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003061 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003062}
3063
3064static void
3065weston_subsurface_commit(struct weston_subsurface *sub)
3066{
3067 struct weston_surface *surface = sub->surface;
3068 struct weston_subsurface *tmp;
3069
3070 /* Recursive check for effectively synchronized. */
3071 if (weston_subsurface_is_synchronized(sub)) {
3072 weston_subsurface_commit_to_cache(sub);
3073 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003074 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003075 /* flush accumulated state from cache */
3076 weston_subsurface_commit_to_cache(sub);
3077 weston_subsurface_commit_from_cache(sub);
3078 } else {
3079 weston_surface_commit(surface);
3080 }
3081
3082 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3083 if (tmp->surface != surface)
3084 weston_subsurface_parent_commit(tmp, 0);
3085 }
3086 }
3087}
3088
3089static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003090weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003091{
3092 struct weston_surface *surface = sub->surface;
3093 struct weston_subsurface *tmp;
3094
Pekka Paalanene67858b2013-04-25 13:57:42 +03003095 /* From now on, commit_from_cache the whole sub-tree, regardless of
3096 * the synchronized mode of each child. This sub-surface or some
3097 * of its ancestors were synchronized, so we are synchronized
3098 * all the way down.
3099 */
3100
Jason Ekstrand7b982072014-05-20 14:33:03 -05003101 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003102 weston_subsurface_commit_from_cache(sub);
3103
3104 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3105 if (tmp->surface != surface)
3106 weston_subsurface_parent_commit(tmp, 1);
3107 }
3108}
3109
3110static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003111weston_subsurface_parent_commit(struct weston_subsurface *sub,
3112 int parent_is_synchronized)
3113{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003114 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003115 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003116 wl_list_for_each(view, &sub->surface->views, surface_link)
3117 weston_view_set_position(view,
3118 sub->position.x,
3119 sub->position.y);
3120
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003121 sub->position.set = 0;
3122 }
3123
3124 if (parent_is_synchronized || sub->synchronized)
3125 weston_subsurface_synchronized_commit(sub);
3126}
3127
Pekka Paalanen8274d902014-08-06 19:36:51 +03003128static int
3129subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3130{
3131 return snprintf(buf, len, "sub-surface");
3132}
3133
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003134static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003135subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003136{
3137 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003138 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003139
Jason Ekstranda7af7042013-10-12 22:38:11 -05003140 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003141 weston_view_set_position(view,
3142 view->geometry.x + dx,
3143 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003144
3145 /* No need to check parent mappedness, because if parent is not
3146 * mapped, parent is not in a visible layer, so this sub-surface
3147 * will not be drawn either.
3148 */
3149 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003150 struct weston_output *output;
3151
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003152 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003153 * because that would call it also for the parent surface,
3154 * which might not be mapped yet. That would lead to
3155 * inconsistent state, where the window could never be
3156 * mapped.
3157 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003158 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003159 * weston_surface_is_mapped() return true, so that when the
3160 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003161 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003162 */
3163 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003164 output = container_of(compositor->output_list.next,
3165 struct weston_output, link);
3166
3167 surface->output = output;
Bryce Harrington89324ce2015-12-23 18:38:07 -08003168 weston_surface_update_output_mask(surface, 1u << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003169 }
3170}
3171
3172static struct weston_subsurface *
3173weston_surface_to_subsurface(struct weston_surface *surface)
3174{
3175 if (surface->configure == subsurface_configure)
3176 return surface->configure_private;
3177
3178 return NULL;
3179}
3180
Pekka Paalanen01388e22013-04-25 13:57:44 +03003181WL_EXPORT struct weston_surface *
3182weston_surface_get_main_surface(struct weston_surface *surface)
3183{
3184 struct weston_subsurface *sub;
3185
3186 while (surface && (sub = weston_surface_to_subsurface(surface)))
3187 surface = sub->parent;
3188
3189 return surface;
3190}
3191
Pekka Paalanen50b67472014-10-01 15:02:41 +03003192WL_EXPORT int
3193weston_surface_set_role(struct weston_surface *surface,
3194 const char *role_name,
3195 struct wl_resource *error_resource,
3196 uint32_t error_code)
3197{
3198 assert(role_name);
3199
3200 if (surface->role_name == NULL ||
3201 surface->role_name == role_name ||
3202 strcmp(surface->role_name, role_name) == 0) {
3203 surface->role_name = role_name;
3204
3205 return 0;
3206 }
3207
3208 wl_resource_post_error(error_resource, error_code,
3209 "Cannot assign role %s to wl_surface@%d,"
3210 " already has role %s\n",
3211 role_name,
3212 wl_resource_get_id(surface->resource),
3213 surface->role_name);
3214 return -1;
3215}
3216
Pekka Paalanen8274d902014-08-06 19:36:51 +03003217WL_EXPORT void
3218weston_surface_set_label_func(struct weston_surface *surface,
3219 int (*desc)(struct weston_surface *,
3220 char *, size_t))
3221{
3222 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003223 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003224}
3225
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003226/** Get the size of surface contents
3227 *
3228 * \param surface The surface to query.
3229 * \param width Returns the width of raw contents.
3230 * \param height Returns the height of raw contents.
3231 *
3232 * Retrieves the raw surface content size in pixels for the given surface.
3233 * This is the whole content size in buffer pixels. If the surface
3234 * has no content or the renderer does not implement this feature,
3235 * zeroes are returned.
3236 *
3237 * This function is used to determine the buffer size needed for
3238 * a weston_surface_copy_content() call.
3239 */
3240WL_EXPORT void
3241weston_surface_get_content_size(struct weston_surface *surface,
3242 int *width, int *height)
3243{
3244 struct weston_renderer *rer = surface->compositor->renderer;
3245
3246 if (!rer->surface_get_content_size) {
3247 *width = 0;
3248 *height = 0;
3249 return;
3250 }
3251
3252 rer->surface_get_content_size(surface, width, height);
3253}
3254
3255/** Copy surface contents to system memory.
3256 *
3257 * \param surface The surface to copy from.
3258 * \param target Pointer to the target memory buffer.
3259 * \param size Size of the target buffer in bytes.
3260 * \param src_x X location on contents to copy from.
3261 * \param src_y Y location on contents to copy from.
3262 * \param width Width in pixels of the area to copy.
3263 * \param height Height in pixels of the area to copy.
3264 * \return 0 for success, -1 for failure.
3265 *
3266 * Surface contents are maintained by the renderer. They can be in a
3267 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3268 * else.
3269 *
3270 * Surface contents are copied into memory pointed to by target,
3271 * which has size bytes of space available. The target memory
3272 * may be larger than needed, but being smaller returns an error.
3273 * The extra bytes in target may or may not be written; their content is
3274 * unspecified. Size must be large enough to hold the image.
3275 *
3276 * The image in the target memory will be arranged in rows from
3277 * top to bottom, and pixels on a row from left to right. The pixel
3278 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3279 * width * 4.
3280 *
3281 * Parameters src_x and src_y define the upper-left corner in buffer
3282 * coordinates (pixels) to copy from. Parameters width and height
3283 * define the size of the area to copy in pixels.
3284 *
3285 * The rectangle defined by src_x, src_y, width, height must fit in
3286 * the surface contents. Otherwise an error is returned.
3287 *
3288 * Use surface_get_data_size to determine the content size; the
3289 * needed target buffer size and rectangle limits.
3290 *
3291 * CURRENT IMPLEMENTATION RESTRICTIONS:
3292 * - the machine must be little-endian due to Pixman formats.
3293 *
3294 * NOTE: Pixman formats are premultiplied.
3295 */
3296WL_EXPORT int
3297weston_surface_copy_content(struct weston_surface *surface,
3298 void *target, size_t size,
3299 int src_x, int src_y,
3300 int width, int height)
3301{
3302 struct weston_renderer *rer = surface->compositor->renderer;
3303 int cw, ch;
3304 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3305
3306 if (!rer->surface_copy_content)
3307 return -1;
3308
3309 weston_surface_get_content_size(surface, &cw, &ch);
3310
3311 if (src_x < 0 || src_y < 0)
3312 return -1;
3313
3314 if (width <= 0 || height <= 0)
3315 return -1;
3316
3317 if (src_x + width > cw || src_y + height > ch)
3318 return -1;
3319
3320 if (width * bytespp * height > size)
3321 return -1;
3322
3323 return rer->surface_copy_content(surface, target, size,
3324 src_x, src_y, width, height);
3325}
3326
Pekka Paalanene67858b2013-04-25 13:57:42 +03003327static void
3328subsurface_set_position(struct wl_client *client,
3329 struct wl_resource *resource, int32_t x, int32_t y)
3330{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003331 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003332
3333 if (!sub)
3334 return;
3335
3336 sub->position.x = x;
3337 sub->position.y = y;
3338 sub->position.set = 1;
3339}
3340
3341static struct weston_subsurface *
3342subsurface_from_surface(struct weston_surface *surface)
3343{
3344 struct weston_subsurface *sub;
3345
3346 sub = weston_surface_to_subsurface(surface);
3347 if (sub)
3348 return sub;
3349
3350 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3351 if (sub->surface == surface)
3352 return sub;
3353
3354 return NULL;
3355}
3356
3357static struct weston_subsurface *
3358subsurface_sibling_check(struct weston_subsurface *sub,
3359 struct weston_surface *surface,
3360 const char *request)
3361{
3362 struct weston_subsurface *sibling;
3363
3364 sibling = subsurface_from_surface(surface);
3365
3366 if (!sibling) {
3367 wl_resource_post_error(sub->resource,
3368 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3369 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003370 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003371 return NULL;
3372 }
3373
3374 if (sibling->parent != sub->parent) {
3375 wl_resource_post_error(sub->resource,
3376 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3377 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003378 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003379 return NULL;
3380 }
3381
3382 return sibling;
3383}
3384
3385static void
3386subsurface_place_above(struct wl_client *client,
3387 struct wl_resource *resource,
3388 struct wl_resource *sibling_resource)
3389{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003390 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003391 struct weston_surface *surface =
3392 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003393 struct weston_subsurface *sibling;
3394
3395 if (!sub)
3396 return;
3397
3398 sibling = subsurface_sibling_check(sub, surface, "place_above");
3399 if (!sibling)
3400 return;
3401
3402 wl_list_remove(&sub->parent_link_pending);
3403 wl_list_insert(sibling->parent_link_pending.prev,
3404 &sub->parent_link_pending);
3405}
3406
3407static void
3408subsurface_place_below(struct wl_client *client,
3409 struct wl_resource *resource,
3410 struct wl_resource *sibling_resource)
3411{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003412 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003413 struct weston_surface *surface =
3414 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003415 struct weston_subsurface *sibling;
3416
3417 if (!sub)
3418 return;
3419
3420 sibling = subsurface_sibling_check(sub, surface, "place_below");
3421 if (!sibling)
3422 return;
3423
3424 wl_list_remove(&sub->parent_link_pending);
3425 wl_list_insert(&sibling->parent_link_pending,
3426 &sub->parent_link_pending);
3427}
3428
3429static void
3430subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3431{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003432 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003433
3434 if (sub)
3435 sub->synchronized = 1;
3436}
3437
3438static void
3439subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3440{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003441 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003442
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003443 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003444 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003445
3446 /* If sub became effectively desynchronized, flush. */
3447 if (!weston_subsurface_is_synchronized(sub))
3448 weston_subsurface_synchronized_commit(sub);
3449 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003450}
3451
3452static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003453weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3454{
3455 wl_list_remove(&sub->parent_link);
3456 wl_list_remove(&sub->parent_link_pending);
3457 wl_list_remove(&sub->parent_destroy_listener.link);
3458 sub->parent = NULL;
3459}
3460
3461static void
3462weston_subsurface_destroy(struct weston_subsurface *sub);
3463
3464static void
3465subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3466{
3467 struct weston_subsurface *sub =
3468 container_of(listener, struct weston_subsurface,
3469 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003470 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003471
3472 /* The protocol object (wl_resource) is left inert. */
3473 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003474 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003475
3476 weston_subsurface_destroy(sub);
3477}
3478
3479static void
3480subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3481{
3482 struct weston_subsurface *sub =
3483 container_of(listener, struct weston_subsurface,
3484 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003485 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003486 assert(sub->surface != sub->parent);
3487
3488 if (weston_surface_is_mapped(sub->surface))
3489 weston_surface_unmap(sub->surface);
3490
3491 weston_subsurface_unlink_parent(sub);
3492}
3493
3494static void
3495subsurface_resource_destroy(struct wl_resource *resource)
3496{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003497 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003498
3499 if (sub)
3500 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003501}
3502
3503static void
3504subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3505{
3506 wl_resource_destroy(resource);
3507}
3508
3509static void
3510weston_subsurface_link_parent(struct weston_subsurface *sub,
3511 struct weston_surface *parent)
3512{
3513 sub->parent = parent;
3514 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003515 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003516 &sub->parent_destroy_listener);
3517
3518 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3519 wl_list_insert(&parent->subsurface_list_pending,
3520 &sub->parent_link_pending);
3521}
3522
3523static void
3524weston_subsurface_link_surface(struct weston_subsurface *sub,
3525 struct weston_surface *surface)
3526{
3527 sub->surface = surface;
3528 sub->surface_destroy_listener.notify =
3529 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003530 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003531 &sub->surface_destroy_listener);
3532}
3533
3534static void
3535weston_subsurface_destroy(struct weston_subsurface *sub)
3536{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003537 struct weston_view *view, *next;
3538
Pekka Paalanene67858b2013-04-25 13:57:42 +03003539 assert(sub->surface);
3540
3541 if (sub->resource) {
3542 assert(weston_surface_to_subsurface(sub->surface) == sub);
3543 assert(sub->parent_destroy_listener.notify ==
3544 subsurface_handle_parent_destroy);
3545
George Kiagiadakised04d382014-06-13 18:10:26 +02003546 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3547 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003548 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003549 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003550
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551 if (sub->parent)
3552 weston_subsurface_unlink_parent(sub);
3553
Jason Ekstrand7b982072014-05-20 14:33:03 -05003554 weston_surface_state_fini(&sub->cached);
3555 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003556
3557 sub->surface->configure = NULL;
3558 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003559 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003560 } else {
3561 /* the dummy weston_subsurface for the parent itself */
3562 assert(sub->parent_destroy_listener.notify == NULL);
3563 wl_list_remove(&sub->parent_link);
3564 wl_list_remove(&sub->parent_link_pending);
3565 }
3566
3567 wl_list_remove(&sub->surface_destroy_listener.link);
3568 free(sub);
3569}
3570
3571static const struct wl_subsurface_interface subsurface_implementation = {
3572 subsurface_destroy,
3573 subsurface_set_position,
3574 subsurface_place_above,
3575 subsurface_place_below,
3576 subsurface_set_sync,
3577 subsurface_set_desync
3578};
3579
3580static struct weston_subsurface *
3581weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3582 struct weston_surface *parent)
3583{
3584 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003585 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003586
Bryce Harringtonde16d892014-11-20 22:21:57 -08003587 sub = zalloc(sizeof *sub);
3588 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003589 return NULL;
3590
Jason Ekstranda7af7042013-10-12 22:38:11 -05003591 wl_list_init(&sub->unused_views);
3592
Jason Ekstranda85118c2013-06-27 20:17:02 -05003593 sub->resource =
3594 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003595 if (!sub->resource) {
3596 free(sub);
3597 return NULL;
3598 }
3599
Jason Ekstranda85118c2013-06-27 20:17:02 -05003600 wl_resource_set_implementation(sub->resource,
3601 &subsurface_implementation,
3602 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003603 weston_subsurface_link_surface(sub, surface);
3604 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003605 weston_surface_state_init(&sub->cached);
3606 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003607 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003608
3609 return sub;
3610}
3611
3612/* Create a dummy subsurface for having the parent itself in its
3613 * sub-surface lists. Makes stacking order manipulation easy.
3614 */
3615static struct weston_subsurface *
3616weston_subsurface_create_for_parent(struct weston_surface *parent)
3617{
3618 struct weston_subsurface *sub;
3619
Bryce Harringtonde16d892014-11-20 22:21:57 -08003620 sub = zalloc(sizeof *sub);
3621 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003622 return NULL;
3623
3624 weston_subsurface_link_surface(sub, parent);
3625 sub->parent = parent;
3626 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3627 wl_list_insert(&parent->subsurface_list_pending,
3628 &sub->parent_link_pending);
3629
3630 return sub;
3631}
3632
3633static void
3634subcompositor_get_subsurface(struct wl_client *client,
3635 struct wl_resource *resource,
3636 uint32_t id,
3637 struct wl_resource *surface_resource,
3638 struct wl_resource *parent_resource)
3639{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003640 struct weston_surface *surface =
3641 wl_resource_get_user_data(surface_resource);
3642 struct weston_surface *parent =
3643 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003644 struct weston_subsurface *sub;
3645 static const char where[] = "get_subsurface: wl_subsurface@";
3646
3647 if (surface == parent) {
3648 wl_resource_post_error(resource,
3649 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3650 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003651 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003652 return;
3653 }
3654
3655 if (weston_surface_to_subsurface(surface)) {
3656 wl_resource_post_error(resource,
3657 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3658 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003659 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003660 return;
3661 }
3662
Pekka Paalanen50b67472014-10-01 15:02:41 +03003663 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3664 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003665 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003666
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003667 if (weston_surface_get_main_surface(parent) == surface) {
3668 wl_resource_post_error(resource,
3669 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3670 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003671 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003672 return;
3673 }
3674
Pekka Paalanene67858b2013-04-25 13:57:42 +03003675 /* make sure the parent is in its own list */
3676 if (wl_list_empty(&parent->subsurface_list)) {
3677 if (!weston_subsurface_create_for_parent(parent)) {
3678 wl_resource_post_no_memory(resource);
3679 return;
3680 }
3681 }
3682
3683 sub = weston_subsurface_create(id, surface, parent);
3684 if (!sub) {
3685 wl_resource_post_no_memory(resource);
3686 return;
3687 }
3688
3689 surface->configure = subsurface_configure;
3690 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003691 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003692}
3693
3694static void
3695subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3696{
3697 wl_resource_destroy(resource);
3698}
3699
3700static const struct wl_subcompositor_interface subcompositor_interface = {
3701 subcompositor_destroy,
3702 subcompositor_get_subsurface
3703};
3704
3705static void
3706bind_subcompositor(struct wl_client *client,
3707 void *data, uint32_t version, uint32_t id)
3708{
3709 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003710 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003711
Jason Ekstranda85118c2013-06-27 20:17:02 -05003712 resource =
3713 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003714 if (resource == NULL) {
3715 wl_client_post_no_memory(client);
3716 return;
3717 }
3718 wl_resource_set_implementation(resource, &subcompositor_interface,
3719 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003720}
3721
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003722/** Set a DPMS mode on all of the compositor's outputs
3723 *
3724 * \param compositor The compositor instance
3725 * \param state The DPMS state the outputs will be set to
3726 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003727static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003728weston_compositor_dpms(struct weston_compositor *compositor,
3729 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003730{
3731 struct weston_output *output;
3732
3733 wl_list_for_each(output, &compositor->output_list, link)
3734 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003735 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003736}
3737
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003738/** Restores the compositor to active status
3739 *
3740 * \param compositor The compositor instance
3741 *
3742 * If the compositor was in a sleeping mode, all outputs are powered
3743 * back on via DPMS. Otherwise if the compositor was inactive
3744 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3745 * signal will fire.
3746 *
3747 * Restarts the idle timer.
3748 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003749WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003750weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003751{
Neil Roberts8b62e202013-09-30 13:14:47 +01003752 uint32_t old_state = compositor->state;
3753
3754 /* The state needs to be changed before emitting the wake
3755 * signal because that may try to schedule a repaint which
3756 * will not work if the compositor is still sleeping */
3757 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3758
3759 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003760 case WESTON_COMPOSITOR_SLEEPING:
3761 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3762 /* fall through */
3763 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003764 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003765 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003766 /* fall through */
3767 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003768 wl_event_source_timer_update(compositor->idle_source,
3769 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003770 }
3771}
3772
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003773/** Turns off rendering and frame events for the compositor.
3774 *
3775 * \param compositor The compositor instance
3776 *
3777 * This is used for example to prevent further rendering while the
3778 * compositor is shutting down.
3779 *
3780 * \note When offscreen state is entered, outputs will be powered
3781 * back on if they were sleeping (in DPMS off mode), even though
3782 * no rendering will be performed.
3783 *
3784 * Stops the idle timer.
3785 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003786WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003787weston_compositor_offscreen(struct weston_compositor *compositor)
3788{
3789 switch (compositor->state) {
3790 case WESTON_COMPOSITOR_OFFSCREEN:
3791 return;
3792 case WESTON_COMPOSITOR_SLEEPING:
3793 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3794 /* fall through */
3795 default:
3796 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3797 wl_event_source_timer_update(compositor->idle_source, 0);
3798 }
3799}
3800
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003801/** Powers down all attached output devices
3802 *
3803 * \param compositor The compositor instance
3804 *
3805 * Causes rendering to the outputs to cease, and no frame events to be
3806 * sent. Only powers down the outputs if the compositor is not already
3807 * in sleep mode.
3808 *
3809 * Stops the idle timer.
3810 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003811WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003812weston_compositor_sleep(struct weston_compositor *compositor)
3813{
3814 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3815 return;
3816
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003817 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003818 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3819 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3820}
3821
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003822/** Sets compositor to idle mode
3823 *
3824 * \param data The compositor instance
3825 *
3826 * This is called when the idle timer fires. Once the compositor is in
3827 * idle mode it requires a wake action (e.g. via
3828 * weston_compositor_wake()) to restore it. The compositor's
3829 * idle_signal will be triggered when the idle event occurs.
3830 *
3831 * Idleness can be inhibited by setting the compositor's idle_inhibit
3832 * property.
3833 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003834static int
3835idle_handler(void *data)
3836{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003837 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003838
3839 if (compositor->idle_inhibit)
3840 return 1;
3841
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003842 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003843 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003844
3845 return 1;
3846}
3847
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003848WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003849weston_plane_init(struct weston_plane *plane,
3850 struct weston_compositor *ec,
3851 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003852{
3853 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003854 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003855 plane->x = x;
3856 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003857 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003858
3859 /* Init the link so that the call to wl_list_remove() when releasing
3860 * the plane without ever stacking doesn't lead to a crash */
3861 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003862}
3863
3864WL_EXPORT void
3865weston_plane_release(struct weston_plane *plane)
3866{
Xiong Zhang97116532013-10-23 13:58:31 +08003867 struct weston_view *view;
3868
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003869 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003870 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003871
Xiong Zhang97116532013-10-23 13:58:31 +08003872 wl_list_for_each(view, &plane->compositor->view_list, link) {
3873 if (view->plane == plane)
3874 view->plane = NULL;
3875 }
3876
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003877 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003878}
3879
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003880WL_EXPORT void
3881weston_compositor_stack_plane(struct weston_compositor *ec,
3882 struct weston_plane *plane,
3883 struct weston_plane *above)
3884{
3885 if (above)
3886 wl_list_insert(above->link.prev, &plane->link);
3887 else
3888 wl_list_insert(&ec->plane_list, &plane->link);
3889}
3890
Casey Dahlin9074db52012-04-19 22:50:09 -04003891static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003892{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003893 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003894}
3895
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003896static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003897bind_output(struct wl_client *client,
3898 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003899{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003900 struct weston_output *output = data;
3901 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003902 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003903
Jason Ekstranda85118c2013-06-27 20:17:02 -05003904 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06003905 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003906 if (resource == NULL) {
3907 wl_client_post_no_memory(client);
3908 return;
3909 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003910
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003911 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003912 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003913
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003914 wl_output_send_geometry(resource,
3915 output->x,
3916 output->y,
3917 output->mm_width,
3918 output->mm_height,
3919 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003920 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003921 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003922 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003923 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003924 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003925
3926 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003927 wl_output_send_mode(resource,
3928 mode->flags,
3929 mode->width,
3930 mode->height,
3931 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003932 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003933
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003934 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003935 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003936}
3937
David Fort0de859e2016-05-27 23:22:57 +02003938/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003939static void
David Fort0de859e2016-05-27 23:22:57 +02003940weston_compositor_reflow_outputs(struct weston_compositor *compositor,
3941 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003942{
3943 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02003944 bool start_resizing = false;
3945
3946 if (!delta_width)
3947 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003948
3949 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02003950 if (output == resized_output) {
3951 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003952 continue;
3953 }
3954
David Fort0de859e2016-05-27 23:22:57 +02003955 if (start_resizing) {
3956 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003957 output->dirty = 1;
3958 }
3959 }
3960}
3961
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003962WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003963weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003964{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003965 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003966 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003967
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003968 output->destroying = 1;
3969
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003970 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08003971 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03003972 weston_view_assign_output(view);
3973 }
3974
Pekka Paalanen0513a952014-05-21 16:17:27 +03003975 wl_event_source_remove(output->repaint_timer);
3976
Pekka Paalanen133e4392014-09-23 22:08:46 -04003977 weston_presentation_feedback_discard_list(&output->feedback_list);
3978
David Fort0de859e2016-05-27 23:22:57 +02003979 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003980 wl_list_remove(&output->link);
3981
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003982 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003983 wl_signal_emit(&output->destroy_signal, output);
3984
Richard Hughesafe690c2013-05-02 10:10:04 +01003985 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003986 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003987 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08003988 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003989
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003990 wl_resource_for_each(resource, &output->resource_list) {
3991 wl_resource_set_destructor(resource, NULL);
3992 }
3993
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003994 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003995}
3996
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003997WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003998weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003999{
Scott Moreau850ca422012-05-21 15:21:25 -06004000 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004001
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004002 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004003 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004004
Scott Moreauccbf29d2012-02-22 14:21:41 -07004005 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004006 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004007 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004008 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004009 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004010 weston_matrix_scale(&output->matrix, magnification,
4011 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004012 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004013
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004014 switch (output->transform) {
4015 case WL_OUTPUT_TRANSFORM_FLIPPED:
4016 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4017 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4018 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4019 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4020 weston_matrix_scale(&output->matrix, -1, 1, 1);
4021 break;
4022 }
4023
4024 switch (output->transform) {
4025 default:
4026 case WL_OUTPUT_TRANSFORM_NORMAL:
4027 case WL_OUTPUT_TRANSFORM_FLIPPED:
4028 break;
4029 case WL_OUTPUT_TRANSFORM_90:
4030 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4031 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4032 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4033 break;
4034 case WL_OUTPUT_TRANSFORM_180:
4035 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4036 weston_matrix_translate(&output->matrix,
4037 -output->width, -output->height, 0);
4038 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4039 break;
4040 case WL_OUTPUT_TRANSFORM_270:
4041 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4042 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4043 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4044 break;
4045 }
4046
4047 if (output->current_scale != 1)
4048 weston_matrix_scale(&output->matrix,
4049 output->current_scale,
4050 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004051
Scott Moreauccbf29d2012-02-22 14:21:41 -07004052 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004053
4054 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004055}
4056
Scott Moreau1bad5db2012-08-18 01:04:05 -06004057static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004058weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004059{
4060 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004061 output->native_scale = scale;
4062 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004063
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004064 convert_size_by_transform_scale(&output->width, &output->height,
4065 output->current_mode->width,
4066 output->current_mode->height,
4067 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004068}
4069
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004070static void
4071weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004072{
4073 output->x = x;
4074 output->y = y;
4075
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004076 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004077 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004078 output->width,
4079 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004080}
4081
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004082WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004083weston_output_move(struct weston_output *output, int x, int y)
4084{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004085 struct wl_resource *resource;
4086
4087 output->move_x = x - output->x;
4088 output->move_y = y - output->y;
4089
4090 if (output->move_x == 0 && output->move_y == 0)
4091 return;
4092
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004093 weston_output_init_geometry(output, x, y);
4094
4095 output->dirty = 1;
4096
4097 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004098 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004099
4100 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004101 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004102 wl_output_send_geometry(resource,
4103 output->x,
4104 output->y,
4105 output->mm_width,
4106 output->mm_height,
4107 output->subpixel,
4108 output->make,
4109 output->model,
4110 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004111
FORT David8a120692016-04-26 23:34:06 +02004112 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004113 wl_output_send_done(resource);
4114 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004115}
4116
Bryce Harrington3f650b82015-12-23 11:01:58 -08004117/** Initialize a weston_output object's parameters
4118 *
4119 * \param output The weston_output object to initialize
4120 * \param c The output's compositor
4121 * \param x x coordinate for the output in global coordinate space
4122 * \param y y coordinate for the output in global coordinate space
4123 * \param mm_width Physical width of the output as reported by the backend
4124 * \param mm_height Physical height of the output as reported by the backend
4125 * \param transform Rotation of the output
4126 * \param scale Native scaling factor for the output
4127 *
4128 * Sets up the transformation, zoom, and geometry of the output using
4129 * the input properties.
4130 *
4131 * Establishes a repaint timer for the output with the relevant display
4132 * object's event loop. See output_repaint_timer_handler().
4133 *
4134 * The output is assigned an ID. Weston can support up to 32 distinct
4135 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4136 * is referred to and used to find the first available ID number, and
4137 * then this ID is marked as used in output_id_pool.
4138 *
4139 * The output is also assigned a Wayland global with the wl_output
4140 * external interface.
4141 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004142WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004143weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004144 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004145 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004146{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004147 struct wl_event_loop *loop;
4148
Bryce Harrington18e45732015-12-23 20:53:53 -08004149 /* Verify we haven't reached the limit of 32 available output IDs */
4150 assert(ffs(~c->output_id_pool) > 0);
4151
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004152 output->compositor = c;
4153 output->x = x;
4154 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004155 output->mm_width = mm_width;
4156 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004157 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004158 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004159
Alexander Larsson0b135062013-05-28 16:23:36 +02004160 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004161 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004162
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004163 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004164 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004165
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004166 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004167 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004168 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004169 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004170 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004171 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004172
Pekka Paalanen0513a952014-05-21 16:17:27 +03004173 loop = wl_display_get_event_loop(c->wl_display);
4174 output->repaint_timer = wl_event_loop_add_timer(loop,
4175 output_repaint_timer_handler, output);
4176
Bryce Harrington3f650b82015-12-23 11:01:58 -08004177 /* Invert the output id pool and look for the lowest numbered
4178 * switch (the least significant bit). Take that bit's position
4179 * as our ID, and mark it used in the compositor's output_id_pool.
4180 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004181 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004182 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004183
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004184 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004185 wl_global_create(c->wl_display, &wl_output_interface, 2,
4186 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004187}
4188
4189/** Adds an output to the compositor's output list and
4190 * send the compositor's output_created signal.
4191 *
4192 * \param compositor The compositor instance.
4193 * \param output The output to be added.
4194 */
4195WL_EXPORT void
4196weston_compositor_add_output(struct weston_compositor *compositor,
4197 struct weston_output *output)
4198{
4199 wl_list_insert(compositor->output_list.prev, &output->link);
4200 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004201}
4202
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004203WL_EXPORT void
4204weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004205 double device_x, double device_y,
4206 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004207{
Derek Foreman0f679412014-10-02 13:41:17 -05004208 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004209 device_x,
4210 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004211 0.0,
4212 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004213
Derek Foreman67a18b92015-03-24 11:36:14 -05004214 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004215
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004216 *x = p.f[0] / p.f[3];
4217 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004218}
4219
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004220static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004221destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004222{
Jonny Lamb74130762013-11-26 18:19:46 +01004223 struct weston_surface *surface =
4224 wl_resource_get_user_data(resource);
4225
Pekka Paalanen4826f872016-04-22 14:14:38 +03004226 if (!surface)
4227 return;
4228
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004229 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004230 surface->pending.buffer_viewport.buffer.src_width =
4231 wl_fixed_from_int(-1);
4232 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004233 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004234}
4235
4236static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004237viewport_destroy(struct wl_client *client,
4238 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004239{
4240 wl_resource_destroy(resource);
4241}
4242
4243static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004244viewport_set_source(struct wl_client *client,
4245 struct wl_resource *resource,
4246 wl_fixed_t src_x,
4247 wl_fixed_t src_y,
4248 wl_fixed_t src_width,
4249 wl_fixed_t src_height)
4250{
4251 struct weston_surface *surface =
4252 wl_resource_get_user_data(resource);
4253
Pekka Paalanen4826f872016-04-22 14:14:38 +03004254 if (!surface) {
4255 wl_resource_post_error(resource,
4256 WP_VIEWPORT_ERROR_NO_SURFACE,
4257 "wl_surface for this viewport is no longer exists");
4258 return;
4259 }
4260
4261 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004262
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004263 if (src_width == wl_fixed_from_int(-1) &&
4264 src_height == wl_fixed_from_int(-1)) {
4265 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004266 surface->pending.buffer_viewport.buffer.src_width =
4267 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004268 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004269 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004270 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004271
4272 if (src_width <= 0 || src_height <= 0) {
4273 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004274 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004275 "source size must be positive (%fx%f)",
4276 wl_fixed_to_double(src_width),
4277 wl_fixed_to_double(src_height));
4278 return;
4279 }
4280
4281 surface->pending.buffer_viewport.buffer.src_x = src_x;
4282 surface->pending.buffer_viewport.buffer.src_y = src_y;
4283 surface->pending.buffer_viewport.buffer.src_width = src_width;
4284 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004285 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004286}
4287
4288static void
4289viewport_set_destination(struct wl_client *client,
4290 struct wl_resource *resource,
4291 int32_t dst_width,
4292 int32_t dst_height)
4293{
4294 struct weston_surface *surface =
4295 wl_resource_get_user_data(resource);
4296
Pekka Paalanen4826f872016-04-22 14:14:38 +03004297 if (!surface) {
4298 wl_resource_post_error(resource,
4299 WP_VIEWPORT_ERROR_NO_SURFACE,
4300 "wl_surface for this viewport no longer exists");
4301 return;
4302 }
4303
4304 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004305
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004306 if (dst_width == -1 && dst_height == -1) {
4307 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004308 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004309 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004310 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004311 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004312
4313 if (dst_width <= 0 || dst_height <= 0) {
4314 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004315 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004316 "destination size must be positive (%dx%d)",
4317 dst_width, dst_height);
4318 return;
4319 }
4320
4321 surface->pending.buffer_viewport.surface.width = dst_width;
4322 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004323 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004324}
4325
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004326static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004327 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004328 viewport_set_source,
4329 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004330};
4331
4332static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004333viewporter_destroy(struct wl_client *client,
4334 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004335{
4336 wl_resource_destroy(resource);
4337}
4338
4339static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004340viewporter_get_viewport(struct wl_client *client,
4341 struct wl_resource *viewporter,
4342 uint32_t id,
4343 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004344{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004345 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004346 struct weston_surface *surface =
4347 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004348 struct wl_resource *resource;
4349
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004350 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004351 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004352 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004353 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004354 return;
4355 }
4356
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004357 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004358 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004359 if (resource == NULL) {
4360 wl_client_post_no_memory(client);
4361 return;
4362 }
4363
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004364 wl_resource_set_implementation(resource, &viewport_interface,
4365 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004366
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004367 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004368}
4369
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004370static const struct wp_viewporter_interface viewporter_interface = {
4371 viewporter_destroy,
4372 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004373};
4374
4375static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004376bind_viewporter(struct wl_client *client,
4377 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004378{
4379 struct wl_resource *resource;
4380
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004381 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004382 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004383 if (resource == NULL) {
4384 wl_client_post_no_memory(client);
4385 return;
4386 }
4387
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004388 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004389 NULL, NULL);
4390}
4391
4392static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004393destroy_presentation_feedback(struct wl_resource *feedback_resource)
4394{
4395 struct weston_presentation_feedback *feedback;
4396
4397 feedback = wl_resource_get_user_data(feedback_resource);
4398
4399 wl_list_remove(&feedback->link);
4400 free(feedback);
4401}
4402
4403static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004404presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4405{
4406 wl_resource_destroy(resource);
4407}
4408
4409static void
4410presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004411 struct wl_resource *presentation_resource,
4412 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004413 uint32_t callback)
4414{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004415 struct weston_surface *surface;
4416 struct weston_presentation_feedback *feedback;
4417
4418 surface = wl_resource_get_user_data(surface_resource);
4419
Bryce Harringtonde16d892014-11-20 22:21:57 -08004420 feedback = zalloc(sizeof *feedback);
4421 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004422 goto err_calloc;
4423
4424 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004425 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004426 1, callback);
4427 if (!feedback->resource)
4428 goto err_create;
4429
4430 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4431 destroy_presentation_feedback);
4432
4433 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4434
4435 return;
4436
4437err_create:
4438 free(feedback);
4439
4440err_calloc:
4441 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004442}
4443
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004444static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004445 presentation_destroy,
4446 presentation_feedback
4447};
4448
4449static void
4450bind_presentation(struct wl_client *client,
4451 void *data, uint32_t version, uint32_t id)
4452{
4453 struct weston_compositor *compositor = data;
4454 struct wl_resource *resource;
4455
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004456 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004457 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004458 if (resource == NULL) {
4459 wl_client_post_no_memory(client);
4460 return;
4461 }
4462
4463 wl_resource_set_implementation(resource, &presentation_implementation,
4464 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004465 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004466}
4467
4468static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004469compositor_bind(struct wl_client *client,
4470 void *data, uint32_t version, uint32_t id)
4471{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004472 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004473 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004474
Jason Ekstranda85118c2013-06-27 20:17:02 -05004475 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004476 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004477 if (resource == NULL) {
4478 wl_client_post_no_memory(client);
4479 return;
4480 }
4481
4482 wl_resource_set_implementation(resource, &compositor_interface,
4483 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004484}
4485
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004486WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004487weston_environment_get_fd(const char *env)
4488{
4489 char *e, *end;
4490 int fd, flags;
4491
4492 e = getenv(env);
4493 if (!e)
4494 return -1;
4495 fd = strtol(e, &end, 0);
4496 if (*end != '\0')
4497 return -1;
4498
4499 flags = fcntl(fd, F_GETFD);
4500 if (flags == -1)
4501 return -1;
4502
4503 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4504 unsetenv(env);
4505
4506 return fd;
4507}
4508
Pekka Paalanenb5026542014-11-12 15:09:24 +02004509static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004510timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004511 uint32_t key, void *data)
4512{
4513 struct weston_compositor *compositor = data;
4514
4515 if (weston_timeline_enabled_)
4516 weston_timeline_close();
4517 else
4518 weston_timeline_open(compositor);
4519}
4520
Giulio Camuffo459137b2014-10-11 23:56:24 +03004521/** Create the compositor.
4522 *
4523 * This functions creates and initializes a compositor instance.
4524 *
4525 * \param display The Wayland display to be used.
4526 * \param user_data A pointer to an object that can later be retrieved
4527 * using the \ref weston_compositor_get_user_data function.
4528 * \return The compositor instance on success or NULL on failure.
4529 */
4530WL_EXPORT struct weston_compositor *
4531weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004532{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004533 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004534 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004535
Giulio Camuffo459137b2014-10-11 23:56:24 +03004536 ec = zalloc(sizeof *ec);
4537 if (!ec)
4538 return NULL;
4539
4540 ec->wl_display = display;
4541 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004542 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004543 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004544 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004545 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004546 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004547 wl_signal_init(&ec->idle_signal);
4548 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004549 wl_signal_init(&ec->show_input_panel_signal);
4550 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004551 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004552 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004553 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004554 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004555 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004556 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004557 wl_signal_init(&ec->session_signal);
4558 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004559
Casey Dahlin58ba1372012-04-19 22:50:08 -04004560 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004561 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004562
Derek Foreman152254b2015-11-26 14:17:48 -06004563 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004564 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004565 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004566
Giulio Camuffo954f1832014-10-11 18:27:30 +03004567 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004568 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004569 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004570
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004571 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004572 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004573 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004574
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004575 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004576 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004577 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004578
Jason Ekstranda7af7042013-10-12 22:38:11 -05004579 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004580 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004581 wl_list_init(&ec->layer_list);
4582 wl_list_init(&ec->seat_list);
4583 wl_list_init(&ec->output_list);
4584 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004585 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004586 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004587 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004588 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004589 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004590
Xiong Zhang97116532013-10-23 13:58:31 +08004591 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004592 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004593
Giulio Camuffo459137b2014-10-11 23:56:24 +03004594 wl_data_device_manager_init(ec->wl_display);
4595
4596 wl_display_init_shm(ec->wl_display);
4597
4598 loop = wl_display_get_event_loop(ec->wl_display);
4599 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004600
Giulio Camuffo459137b2014-10-11 23:56:24 +03004601 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4602 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4603
4604 weston_compositor_add_debug_binding(ec, KEY_T,
4605 timeline_key_binding_handler, ec);
4606
Giulio Camuffo459137b2014-10-11 23:56:24 +03004607 return ec;
4608
4609fail:
4610 free(ec);
4611 return NULL;
4612}
4613
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004614WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004615weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004616{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004617 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004618
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004619 wl_event_source_remove(ec->idle_source);
4620
Matt Roper361d2ad2011-08-29 13:52:23 -07004621 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004622 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004623 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004624
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004625 if (ec->renderer)
4626 ec->renderer->destroy(ec);
4627
Daniel Stone325fc2d2012-05-30 16:31:58 +01004628 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004629 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004630 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004631 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004632 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004633 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004634
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004635 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004636}
4637
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004638WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004639weston_compositor_exit_with_code(struct weston_compositor *compositor,
4640 int exit_code)
4641{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004642 if (compositor->exit_code == EXIT_SUCCESS)
4643 compositor->exit_code = exit_code;
4644
Giulio Camuffo459137b2014-10-11 23:56:24 +03004645 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004646}
4647
4648WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004649weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4650 const struct weston_pointer_grab_interface *interface)
4651{
4652 struct weston_seat *seat;
4653
4654 ec->default_pointer_grab = interface;
4655 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004656 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4657
4658 if (pointer)
4659 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004660 }
4661}
4662
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004663WL_EXPORT int
4664weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4665 clockid_t clk_id)
4666{
4667 struct timespec ts;
4668
4669 if (clock_gettime(clk_id, &ts) < 0)
4670 return -1;
4671
4672 compositor->presentation_clock = clk_id;
4673
4674 return 0;
4675}
4676
4677/*
4678 * For choosing the software clock, when the display hardware or API
4679 * does not expose a compatible presentation timestamp.
4680 */
4681WL_EXPORT int
4682weston_compositor_set_presentation_clock_software(
4683 struct weston_compositor *compositor)
4684{
4685 /* In order of preference */
4686 static const clockid_t clocks[] = {
4687 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4688 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4689 CLOCK_MONOTONIC, /* no jumps, may crawl */
4690 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4691 CLOCK_REALTIME /* may jump and crawl */
4692 };
4693 unsigned i;
4694
4695 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4696 if (weston_compositor_set_presentation_clock(compositor,
4697 clocks[i]) == 0)
4698 return 0;
4699
4700 weston_log("Error: no suitable presentation clock available.\n");
4701
4702 return -1;
4703}
4704
Pekka Paalanen662f3842015-03-18 12:17:26 +02004705/** Read the current time from the Presentation clock
4706 *
4707 * \param compositor
4708 * \param ts[out] The current time.
4709 *
4710 * \note Reading the current time in user space is always imprecise to some
4711 * degree.
4712 *
4713 * This function is never meant to fail. If reading the clock does fail,
4714 * an error message is logged and a zero time is returned. Callers are not
4715 * supposed to detect or react to failures.
4716 */
4717WL_EXPORT void
4718weston_compositor_read_presentation_clock(
4719 const struct weston_compositor *compositor,
4720 struct timespec *ts)
4721{
4722 static bool warned;
4723 int ret;
4724
4725 ret = clock_gettime(compositor->presentation_clock, ts);
4726 if (ret < 0) {
4727 ts->tv_sec = 0;
4728 ts->tv_nsec = 0;
4729
4730 if (!warned)
4731 weston_log("Error: failure to read "
4732 "the presentation clock %#x: '%m' (%d)\n",
4733 compositor->presentation_clock, errno);
4734 warned = true;
4735 }
4736}
4737
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004738/** Import dmabuf buffer into current renderer
4739 *
4740 * \param compositor
4741 * \param buffer the dmabuf buffer to import
4742 * \return true on usable buffers, false otherwise
4743 *
4744 * This function tests that the linux_dmabuf_buffer is usable
4745 * for the current renderer. Returns false on unusable buffers. Usually
4746 * usability is tested by importing the dmabufs for composition.
4747 *
4748 * This hook is also used for detecting if the renderer supports
4749 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4750 * supported.
4751 * */
4752WL_EXPORT bool
4753weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4754 struct linux_dmabuf_buffer *buffer)
4755{
4756 struct weston_renderer *renderer;
4757
4758 renderer = compositor->renderer;
4759
4760 if (renderer->import_dmabuf == NULL)
4761 return false;
4762
4763 return renderer->import_dmabuf(compositor, buffer);
4764}
4765
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004766WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004767weston_version(int *major, int *minor, int *micro)
4768{
4769 *major = WESTON_VERSION_MAJOR;
4770 *minor = WESTON_VERSION_MINOR;
4771 *micro = WESTON_VERSION_MICRO;
4772}
4773
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004774WL_EXPORT void *
4775weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004776{
Derek Foreman3f86e502015-06-08 11:46:54 -05004777 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004778 char path[PATH_MAX];
4779 void *module, *init;
4780
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004781 if (name == NULL)
4782 return NULL;
4783
Derek Foreman3f86e502015-06-08 11:46:54 -05004784 if (name[0] != '/') {
4785 if (builddir)
4786 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4787 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03004788 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004789 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004790 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004791 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004792
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004793 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4794 if (module) {
4795 weston_log("Module '%s' already loaded\n", path);
4796 dlclose(module);
4797 return NULL;
4798 }
4799
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004800 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004801 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004802 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004803 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004804 return NULL;
4805 }
4806
4807 init = dlsym(module, entrypoint);
4808 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004809 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004810 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004811 return NULL;
4812 }
4813
4814 return init;
4815}
4816
Giulio Camuffo459137b2014-10-11 23:56:24 +03004817
4818/** Destroys the compositor.
4819 *
4820 * This function cleans up the compositor state and destroys it.
4821 *
4822 * \param compositor The compositor to be destroyed.
4823 */
4824WL_EXPORT void
4825weston_compositor_destroy(struct weston_compositor *compositor)
4826{
4827 /* prevent further rendering while shutting down */
4828 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4829
4830 wl_signal_emit(&compositor->destroy_signal, compositor);
4831
4832 weston_compositor_xkb_destroy(compositor);
4833
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004834 if (compositor->backend)
4835 compositor->backend->destroy(compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004836 free(compositor);
4837}
4838
4839/** Instruct the compositor to exit.
4840 *
4841 * This functions does not directly destroy the compositor object, it merely
4842 * command it to start the tear down process. It is not guaranteed that the
4843 * tear down will happen immediately.
4844 *
4845 * \param compositor The compositor to tear down.
4846 */
4847WL_EXPORT void
4848weston_compositor_exit(struct weston_compositor *compositor)
4849{
4850 compositor->exit(compositor);
4851}
4852
4853/** Return the user data stored in the compositor.
4854 *
4855 * This function returns the user data pointer set with user_data parameter
4856 * to the \ref weston_compositor_create function.
4857 */
4858WL_EXPORT void *
4859weston_compositor_get_user_data(struct weston_compositor *compositor)
4860{
4861 return compositor->user_data;
4862}
Pekka Paalanendd186732016-06-03 14:49:54 +03004863
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004864static const char * const backend_map[] = {
4865 [WESTON_BACKEND_DRM] = "drm-backend.so",
4866 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
4867 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
4868 [WESTON_BACKEND_RDP] = "rdp-backend.so",
4869 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
4870 [WESTON_BACKEND_X11] = "x11-backend.so",
4871};
4872
Pekka Paalanendd186732016-06-03 14:49:54 +03004873/** Load a backend into a weston_compositor
4874 *
4875 * A backend must be loaded to make a weston_compositor work. A backend
4876 * provides input and output capabilities, and determines the renderer to use.
4877 *
4878 * \param compositor A compositor that has not had a backend loaded yet.
4879 * \param backend Name of the backend file.
4880 * \param config_base A pointer to a backend-specific configuration
4881 * structure's 'base' member.
4882 *
4883 * \return 0 on success, or -1 on error.
4884 */
4885WL_EXPORT int
4886weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004887 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03004888 struct weston_backend_config *config_base)
4889{
4890 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03004891 struct weston_backend_config *config_base);
4892
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004893 if (backend < 0 || backend >= ARRAY_LENGTH(backend_map))
4894 return -1;
4895
4896 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03004897 if (!backend_init)
4898 return -1;
4899
Pekka Paalanena256c5e2016-06-03 14:56:18 +03004900 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03004901}