blob: 480b013e5afcd83615f4205c9cb866eecfb4fdea [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
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002673/**
2674 * Compute a + b > c while being safe to overflows.
2675 */
2676static bool
2677fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2678{
2679 return (int64_t)a + (int64_t)b > (int64_t)c;
2680}
2681
2682static bool
2683weston_surface_is_pending_viewport_source_valid(
2684 const struct weston_surface *surface)
2685{
2686 const struct weston_surface_state *pend = &surface->pending;
2687 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2688 int width_from_buffer = 0;
2689 int height_from_buffer = 0;
2690 wl_fixed_t w;
2691 wl_fixed_t h;
2692
2693 /* If viewport source rect is not set, it is always ok. */
2694 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2695 return true;
2696
2697 if (pend->newly_attached) {
2698 if (pend->buffer) {
2699 convert_size_by_transform_scale(&width_from_buffer,
2700 &height_from_buffer,
2701 pend->buffer->width,
2702 pend->buffer->height,
2703 vp->buffer.transform,
2704 vp->buffer.scale);
2705 } else {
2706 /* No buffer: viewport is irrelevant. */
2707 return true;
2708 }
2709 } else {
2710 width_from_buffer = surface->width_from_buffer;
2711 height_from_buffer = surface->height_from_buffer;
2712 }
2713
2714 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2715 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2716
2717 /* No buffer: viewport is irrelevant. */
2718 if (width_from_buffer == 0 || height_from_buffer == 0)
2719 return true;
2720
2721 /* overflow checks for wl_fixed_from_int() */
2722 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2723 return false;
2724 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2725 return false;
2726
2727 w = wl_fixed_from_int(width_from_buffer);
2728 h = wl_fixed_from_int(height_from_buffer);
2729
2730 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2731 return false;
2732 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2733 return false;
2734
2735 return true;
2736}
2737
Derek Foreman152254b2015-11-26 14:17:48 -06002738/* Translate pending damage in buffer co-ordinates to surface
2739 * co-ordinates and union it with a pixman_region32_t.
2740 * This should only be called after the buffer is attached.
2741 */
2742static void
2743apply_damage_buffer(pixman_region32_t *dest,
2744 struct weston_surface *surface,
2745 struct weston_surface_state *state)
2746{
2747 struct weston_buffer *buffer = surface->buffer_ref.buffer;
2748
2749 /* wl_surface.damage_buffer needs to be clipped to the buffer,
2750 * translated into surface co-ordinates and unioned with
2751 * any other surface damage.
2752 * None of this makes sense if there is no buffer though.
2753 */
2754 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
2755 pixman_region32_t buffer_damage;
2756
2757 pixman_region32_intersect_rect(&state->damage_buffer,
2758 &state->damage_buffer,
2759 0, 0, buffer->width,
2760 buffer->height);
2761 pixman_region32_init(&buffer_damage);
2762 weston_matrix_transform_region(&buffer_damage,
2763 &surface->buffer_to_surface_matrix,
2764 &state->damage_buffer);
2765 pixman_region32_union(dest, dest, &buffer_damage);
2766 pixman_region32_fini(&buffer_damage);
2767 }
2768 /* We should clear this on commit even if there was no buffer */
2769 pixman_region32_clear(&state->damage_buffer);
2770}
2771
Jason Ekstrand1e059042014-10-16 10:55:19 -05002772static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002773weston_surface_commit_state(struct weston_surface *surface,
2774 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002775{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002776 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002777 pixman_region32_t opaque;
2778
Alexander Larsson4ea95522013-05-22 14:41:37 +02002779 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002780 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03002781 /* wp_viewport.set_source */
2782 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002783 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002784
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002785 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002786 if (state->newly_attached)
2787 weston_surface_attach(surface, state->buffer);
2788 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002789
Jason Ekstrand1e059042014-10-16 10:55:19 -05002790 weston_surface_build_buffer_matrix(surface,
2791 &surface->surface_to_buffer_matrix);
2792 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2793 &surface->surface_to_buffer_matrix);
2794
Jason Ekstrand7b982072014-05-20 14:33:03 -05002795 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002796 weston_surface_update_size(surface);
2797 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002798 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002799 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002800
Jason Ekstrand7b982072014-05-20 14:33:03 -05002801 state->sx = 0;
2802 state->sy = 0;
2803 state->newly_attached = 0;
2804 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002805
Derek Foreman152254b2015-11-26 14:17:48 -06002806 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002807 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06002808 (pixman_region32_not_empty(&state->damage_surface) ||
2809 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02002810 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06002811
Pekka Paalanen8e159182012-10-10 12:49:25 +03002812 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06002813 &state->damage_surface);
2814
2815 apply_damage_buffer(&surface->damage, surface, state);
2816
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002817 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002818 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06002819 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002820
2821 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002822 pixman_region32_init(&opaque);
2823 pixman_region32_intersect_rect(&opaque, &state->opaque,
2824 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002825
2826 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2827 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002828 wl_list_for_each(view, &surface->views, surface_link)
2829 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002830 }
2831
2832 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002833
2834 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002835 pixman_region32_intersect_rect(&surface->input, &state->input,
2836 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002837
Pekka Paalanenbc106382012-10-10 12:49:31 +03002838 /* wl_surface.frame */
2839 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002840 &state->frame_callback_list);
2841 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002842
2843 /* XXX:
2844 * What should happen with a feedback request, if there
2845 * is no wl_buffer attached for this commit?
2846 */
2847
2848 /* presentation.feedback */
2849 wl_list_insert_list(&surface->feedback_list,
2850 &state->feedback_list);
2851 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002852}
2853
2854static void
2855weston_surface_commit(struct weston_surface *surface)
2856{
2857 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002858
Pekka Paalanene67858b2013-04-25 13:57:42 +03002859 weston_surface_commit_subsurface_order(surface);
2860
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002861 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002862}
2863
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002864static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002865weston_subsurface_commit(struct weston_subsurface *sub);
2866
2867static void
2868weston_subsurface_parent_commit(struct weston_subsurface *sub,
2869 int parent_is_synchronized);
2870
2871static void
2872surface_commit(struct wl_client *client, struct wl_resource *resource)
2873{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002874 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002875 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2876
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002877 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
2878 assert(surface->viewport_resource);
2879
2880 wl_resource_post_error(surface->viewport_resource,
2881 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
2882 "wl_surface@%d has viewport source outside buffer",
2883 wl_resource_get_id(resource));
2884 return;
2885 }
2886
Pekka Paalanene67858b2013-04-25 13:57:42 +03002887 if (sub) {
2888 weston_subsurface_commit(sub);
2889 return;
2890 }
2891
2892 weston_surface_commit(surface);
2893
2894 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2895 if (sub->surface != surface)
2896 weston_subsurface_parent_commit(sub, 0);
2897 }
2898}
2899
2900static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002901surface_set_buffer_transform(struct wl_client *client,
2902 struct wl_resource *resource, int transform)
2903{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002904 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002905
Jonny Lamba55f1392014-05-30 12:07:15 +02002906 /* if wl_output.transform grows more members this will need to be updated. */
2907 if (transform < 0 ||
2908 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2909 wl_resource_post_error(resource,
2910 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2911 "buffer transform must be a valid transform "
2912 "('%d' specified)", transform);
2913 return;
2914 }
2915
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002916 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002917 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002918}
2919
Alexander Larsson4ea95522013-05-22 14:41:37 +02002920static void
2921surface_set_buffer_scale(struct wl_client *client,
2922 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002923 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002924{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002925 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002926
Jonny Lamba55f1392014-05-30 12:07:15 +02002927 if (scale < 1) {
2928 wl_resource_post_error(resource,
2929 WL_SURFACE_ERROR_INVALID_SCALE,
2930 "buffer scale must be at least one "
2931 "('%d' specified)", scale);
2932 return;
2933 }
2934
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002935 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002936 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002937}
2938
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002939static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002940 surface_destroy,
2941 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002942 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002943 surface_frame,
2944 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002945 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002946 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002947 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06002948 surface_set_buffer_scale,
2949 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002950};
2951
2952static void
2953compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002954 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002955{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002956 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002957 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002958
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002959 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002960 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002961 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002962 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002963 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002964
Jason Ekstranda85118c2013-06-27 20:17:02 -05002965 surface->resource =
2966 wl_resource_create(client, &wl_surface_interface,
2967 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002968 if (surface->resource == NULL) {
2969 weston_surface_destroy(surface);
2970 wl_resource_post_no_memory(resource);
2971 return;
2972 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002973 wl_resource_set_implementation(surface->resource, &surface_interface,
2974 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002975
2976 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002977}
2978
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002979static void
2980destroy_region(struct wl_resource *resource)
2981{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002982 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002983
2984 pixman_region32_fini(&region->region);
2985 free(region);
2986}
2987
2988static void
2989region_destroy(struct wl_client *client, struct wl_resource *resource)
2990{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002991 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002992}
2993
2994static void
2995region_add(struct wl_client *client, struct wl_resource *resource,
2996 int32_t x, int32_t y, int32_t width, int32_t height)
2997{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002998 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002999
3000 pixman_region32_union_rect(&region->region, &region->region,
3001 x, y, width, height);
3002}
3003
3004static void
3005region_subtract(struct wl_client *client, struct wl_resource *resource,
3006 int32_t x, int32_t y, int32_t width, int32_t height)
3007{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003008 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003009 pixman_region32_t rect;
3010
3011 pixman_region32_init_rect(&rect, x, y, width, height);
3012 pixman_region32_subtract(&region->region, &region->region, &rect);
3013 pixman_region32_fini(&rect);
3014}
3015
3016static const struct wl_region_interface region_interface = {
3017 region_destroy,
3018 region_add,
3019 region_subtract
3020};
3021
3022static void
3023compositor_create_region(struct wl_client *client,
3024 struct wl_resource *resource, uint32_t id)
3025{
3026 struct weston_region *region;
3027
3028 region = malloc(sizeof *region);
3029 if (region == NULL) {
3030 wl_resource_post_no_memory(resource);
3031 return;
3032 }
3033
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003034 pixman_region32_init(&region->region);
3035
Jason Ekstranda85118c2013-06-27 20:17:02 -05003036 region->resource =
3037 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003038 if (region->resource == NULL) {
3039 free(region);
3040 wl_resource_post_no_memory(resource);
3041 return;
3042 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003043 wl_resource_set_implementation(region->resource, &region_interface,
3044 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003045}
3046
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003047static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003048 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003049 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003050};
3051
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003052static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003053weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3054{
3055 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003056
Jason Ekstrand7b982072014-05-20 14:33:03 -05003057 weston_surface_commit_state(surface, &sub->cached);
3058 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003059
3060 weston_surface_commit_subsurface_order(surface);
3061
3062 weston_surface_schedule_repaint(surface);
3063
Jason Ekstrand7b982072014-05-20 14:33:03 -05003064 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003065}
3066
3067static void
3068weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3069{
3070 struct weston_surface *surface = sub->surface;
3071
3072 /*
3073 * If this commit would cause the surface to move by the
3074 * attach(dx, dy) parameters, the old damage region must be
3075 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003076 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003077 */
Derek Foreman152254b2015-11-26 14:17:48 -06003078 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003079 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003080 pixman_region32_union(&sub->cached.damage_surface,
3081 &sub->cached.damage_surface,
3082 &surface->pending.damage_surface);
3083 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003084
3085 if (surface->pending.newly_attached) {
3086 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003087 weston_surface_state_set_buffer(&sub->cached,
3088 surface->pending.buffer);
3089 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003090 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003091 weston_presentation_feedback_discard_list(
3092 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003093 }
3094 sub->cached.sx += surface->pending.sx;
3095 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003096
Derek Foreman152254b2015-11-26 14:17:48 -06003097 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3098
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003099 sub->cached.buffer_viewport.changed |=
3100 surface->pending.buffer_viewport.changed;
3101 sub->cached.buffer_viewport.buffer =
3102 surface->pending.buffer_viewport.buffer;
3103 sub->cached.buffer_viewport.surface =
3104 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003105
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003106 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003107
3108 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3109
3110 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3111
3112 wl_list_insert_list(&sub->cached.frame_callback_list,
3113 &surface->pending.frame_callback_list);
3114 wl_list_init(&surface->pending.frame_callback_list);
3115
Pekka Paalanen133e4392014-09-23 22:08:46 -04003116 wl_list_insert_list(&sub->cached.feedback_list,
3117 &surface->pending.feedback_list);
3118 wl_list_init(&surface->pending.feedback_list);
3119
Jason Ekstrand7b982072014-05-20 14:33:03 -05003120 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003121}
3122
Derek Foreman280e7dd2014-10-03 13:13:42 -05003123static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003124weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3125{
3126 while (sub) {
3127 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003128 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003129
3130 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003131 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003132
3133 sub = weston_surface_to_subsurface(sub->parent);
3134 }
3135
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003136 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003137}
3138
3139static void
3140weston_subsurface_commit(struct weston_subsurface *sub)
3141{
3142 struct weston_surface *surface = sub->surface;
3143 struct weston_subsurface *tmp;
3144
3145 /* Recursive check for effectively synchronized. */
3146 if (weston_subsurface_is_synchronized(sub)) {
3147 weston_subsurface_commit_to_cache(sub);
3148 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003149 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003150 /* flush accumulated state from cache */
3151 weston_subsurface_commit_to_cache(sub);
3152 weston_subsurface_commit_from_cache(sub);
3153 } else {
3154 weston_surface_commit(surface);
3155 }
3156
3157 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3158 if (tmp->surface != surface)
3159 weston_subsurface_parent_commit(tmp, 0);
3160 }
3161 }
3162}
3163
3164static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003165weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003166{
3167 struct weston_surface *surface = sub->surface;
3168 struct weston_subsurface *tmp;
3169
Pekka Paalanene67858b2013-04-25 13:57:42 +03003170 /* From now on, commit_from_cache the whole sub-tree, regardless of
3171 * the synchronized mode of each child. This sub-surface or some
3172 * of its ancestors were synchronized, so we are synchronized
3173 * all the way down.
3174 */
3175
Jason Ekstrand7b982072014-05-20 14:33:03 -05003176 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003177 weston_subsurface_commit_from_cache(sub);
3178
3179 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3180 if (tmp->surface != surface)
3181 weston_subsurface_parent_commit(tmp, 1);
3182 }
3183}
3184
3185static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003186weston_subsurface_parent_commit(struct weston_subsurface *sub,
3187 int parent_is_synchronized)
3188{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003189 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003190 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003191 wl_list_for_each(view, &sub->surface->views, surface_link)
3192 weston_view_set_position(view,
3193 sub->position.x,
3194 sub->position.y);
3195
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003196 sub->position.set = 0;
3197 }
3198
3199 if (parent_is_synchronized || sub->synchronized)
3200 weston_subsurface_synchronized_commit(sub);
3201}
3202
Pekka Paalanen8274d902014-08-06 19:36:51 +03003203static int
3204subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3205{
3206 return snprintf(buf, len, "sub-surface");
3207}
3208
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003209static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003210subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003211{
3212 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003213 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003214
Jason Ekstranda7af7042013-10-12 22:38:11 -05003215 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003216 weston_view_set_position(view,
3217 view->geometry.x + dx,
3218 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003219
3220 /* No need to check parent mappedness, because if parent is not
3221 * mapped, parent is not in a visible layer, so this sub-surface
3222 * will not be drawn either.
3223 */
3224 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003225 struct weston_output *output;
3226
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003227 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003228 * because that would call it also for the parent surface,
3229 * which might not be mapped yet. That would lead to
3230 * inconsistent state, where the window could never be
3231 * mapped.
3232 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003233 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003234 * weston_surface_is_mapped() return true, so that when the
3235 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003236 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003237 */
3238 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003239 output = container_of(compositor->output_list.next,
3240 struct weston_output, link);
3241
3242 surface->output = output;
Bryce Harrington89324ce2015-12-23 18:38:07 -08003243 weston_surface_update_output_mask(surface, 1u << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003244 }
3245}
3246
3247static struct weston_subsurface *
3248weston_surface_to_subsurface(struct weston_surface *surface)
3249{
3250 if (surface->configure == subsurface_configure)
3251 return surface->configure_private;
3252
3253 return NULL;
3254}
3255
Pekka Paalanen01388e22013-04-25 13:57:44 +03003256WL_EXPORT struct weston_surface *
3257weston_surface_get_main_surface(struct weston_surface *surface)
3258{
3259 struct weston_subsurface *sub;
3260
3261 while (surface && (sub = weston_surface_to_subsurface(surface)))
3262 surface = sub->parent;
3263
3264 return surface;
3265}
3266
Pekka Paalanen50b67472014-10-01 15:02:41 +03003267WL_EXPORT int
3268weston_surface_set_role(struct weston_surface *surface,
3269 const char *role_name,
3270 struct wl_resource *error_resource,
3271 uint32_t error_code)
3272{
3273 assert(role_name);
3274
3275 if (surface->role_name == NULL ||
3276 surface->role_name == role_name ||
3277 strcmp(surface->role_name, role_name) == 0) {
3278 surface->role_name = role_name;
3279
3280 return 0;
3281 }
3282
3283 wl_resource_post_error(error_resource, error_code,
3284 "Cannot assign role %s to wl_surface@%d,"
3285 " already has role %s\n",
3286 role_name,
3287 wl_resource_get_id(surface->resource),
3288 surface->role_name);
3289 return -1;
3290}
3291
Pekka Paalanen8274d902014-08-06 19:36:51 +03003292WL_EXPORT void
3293weston_surface_set_label_func(struct weston_surface *surface,
3294 int (*desc)(struct weston_surface *,
3295 char *, size_t))
3296{
3297 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003298 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003299}
3300
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003301/** Get the size of surface contents
3302 *
3303 * \param surface The surface to query.
3304 * \param width Returns the width of raw contents.
3305 * \param height Returns the height of raw contents.
3306 *
3307 * Retrieves the raw surface content size in pixels for the given surface.
3308 * This is the whole content size in buffer pixels. If the surface
3309 * has no content or the renderer does not implement this feature,
3310 * zeroes are returned.
3311 *
3312 * This function is used to determine the buffer size needed for
3313 * a weston_surface_copy_content() call.
3314 */
3315WL_EXPORT void
3316weston_surface_get_content_size(struct weston_surface *surface,
3317 int *width, int *height)
3318{
3319 struct weston_renderer *rer = surface->compositor->renderer;
3320
3321 if (!rer->surface_get_content_size) {
3322 *width = 0;
3323 *height = 0;
3324 return;
3325 }
3326
3327 rer->surface_get_content_size(surface, width, height);
3328}
3329
3330/** Copy surface contents to system memory.
3331 *
3332 * \param surface The surface to copy from.
3333 * \param target Pointer to the target memory buffer.
3334 * \param size Size of the target buffer in bytes.
3335 * \param src_x X location on contents to copy from.
3336 * \param src_y Y location on contents to copy from.
3337 * \param width Width in pixels of the area to copy.
3338 * \param height Height in pixels of the area to copy.
3339 * \return 0 for success, -1 for failure.
3340 *
3341 * Surface contents are maintained by the renderer. They can be in a
3342 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3343 * else.
3344 *
3345 * Surface contents are copied into memory pointed to by target,
3346 * which has size bytes of space available. The target memory
3347 * may be larger than needed, but being smaller returns an error.
3348 * The extra bytes in target may or may not be written; their content is
3349 * unspecified. Size must be large enough to hold the image.
3350 *
3351 * The image in the target memory will be arranged in rows from
3352 * top to bottom, and pixels on a row from left to right. The pixel
3353 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3354 * width * 4.
3355 *
3356 * Parameters src_x and src_y define the upper-left corner in buffer
3357 * coordinates (pixels) to copy from. Parameters width and height
3358 * define the size of the area to copy in pixels.
3359 *
3360 * The rectangle defined by src_x, src_y, width, height must fit in
3361 * the surface contents. Otherwise an error is returned.
3362 *
3363 * Use surface_get_data_size to determine the content size; the
3364 * needed target buffer size and rectangle limits.
3365 *
3366 * CURRENT IMPLEMENTATION RESTRICTIONS:
3367 * - the machine must be little-endian due to Pixman formats.
3368 *
3369 * NOTE: Pixman formats are premultiplied.
3370 */
3371WL_EXPORT int
3372weston_surface_copy_content(struct weston_surface *surface,
3373 void *target, size_t size,
3374 int src_x, int src_y,
3375 int width, int height)
3376{
3377 struct weston_renderer *rer = surface->compositor->renderer;
3378 int cw, ch;
3379 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3380
3381 if (!rer->surface_copy_content)
3382 return -1;
3383
3384 weston_surface_get_content_size(surface, &cw, &ch);
3385
3386 if (src_x < 0 || src_y < 0)
3387 return -1;
3388
3389 if (width <= 0 || height <= 0)
3390 return -1;
3391
3392 if (src_x + width > cw || src_y + height > ch)
3393 return -1;
3394
3395 if (width * bytespp * height > size)
3396 return -1;
3397
3398 return rer->surface_copy_content(surface, target, size,
3399 src_x, src_y, width, height);
3400}
3401
Pekka Paalanene67858b2013-04-25 13:57:42 +03003402static void
3403subsurface_set_position(struct wl_client *client,
3404 struct wl_resource *resource, int32_t x, int32_t y)
3405{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003406 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003407
3408 if (!sub)
3409 return;
3410
3411 sub->position.x = x;
3412 sub->position.y = y;
3413 sub->position.set = 1;
3414}
3415
3416static struct weston_subsurface *
3417subsurface_from_surface(struct weston_surface *surface)
3418{
3419 struct weston_subsurface *sub;
3420
3421 sub = weston_surface_to_subsurface(surface);
3422 if (sub)
3423 return sub;
3424
3425 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3426 if (sub->surface == surface)
3427 return sub;
3428
3429 return NULL;
3430}
3431
3432static struct weston_subsurface *
3433subsurface_sibling_check(struct weston_subsurface *sub,
3434 struct weston_surface *surface,
3435 const char *request)
3436{
3437 struct weston_subsurface *sibling;
3438
3439 sibling = subsurface_from_surface(surface);
3440
3441 if (!sibling) {
3442 wl_resource_post_error(sub->resource,
3443 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3444 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003445 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003446 return NULL;
3447 }
3448
3449 if (sibling->parent != sub->parent) {
3450 wl_resource_post_error(sub->resource,
3451 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3452 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003453 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003454 return NULL;
3455 }
3456
3457 return sibling;
3458}
3459
3460static void
3461subsurface_place_above(struct wl_client *client,
3462 struct wl_resource *resource,
3463 struct wl_resource *sibling_resource)
3464{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003465 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003466 struct weston_surface *surface =
3467 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003468 struct weston_subsurface *sibling;
3469
3470 if (!sub)
3471 return;
3472
3473 sibling = subsurface_sibling_check(sub, surface, "place_above");
3474 if (!sibling)
3475 return;
3476
3477 wl_list_remove(&sub->parent_link_pending);
3478 wl_list_insert(sibling->parent_link_pending.prev,
3479 &sub->parent_link_pending);
3480}
3481
3482static void
3483subsurface_place_below(struct wl_client *client,
3484 struct wl_resource *resource,
3485 struct wl_resource *sibling_resource)
3486{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003487 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003488 struct weston_surface *surface =
3489 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003490 struct weston_subsurface *sibling;
3491
3492 if (!sub)
3493 return;
3494
3495 sibling = subsurface_sibling_check(sub, surface, "place_below");
3496 if (!sibling)
3497 return;
3498
3499 wl_list_remove(&sub->parent_link_pending);
3500 wl_list_insert(&sibling->parent_link_pending,
3501 &sub->parent_link_pending);
3502}
3503
3504static void
3505subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3506{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003507 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003508
3509 if (sub)
3510 sub->synchronized = 1;
3511}
3512
3513static void
3514subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3515{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003516 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003517
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003518 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003519 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003520
3521 /* If sub became effectively desynchronized, flush. */
3522 if (!weston_subsurface_is_synchronized(sub))
3523 weston_subsurface_synchronized_commit(sub);
3524 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003525}
3526
3527static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003528weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3529{
3530 wl_list_remove(&sub->parent_link);
3531 wl_list_remove(&sub->parent_link_pending);
3532 wl_list_remove(&sub->parent_destroy_listener.link);
3533 sub->parent = NULL;
3534}
3535
3536static void
3537weston_subsurface_destroy(struct weston_subsurface *sub);
3538
3539static void
3540subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3541{
3542 struct weston_subsurface *sub =
3543 container_of(listener, struct weston_subsurface,
3544 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003545 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003546
3547 /* The protocol object (wl_resource) is left inert. */
3548 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003549 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003550
3551 weston_subsurface_destroy(sub);
3552}
3553
3554static void
3555subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3556{
3557 struct weston_subsurface *sub =
3558 container_of(listener, struct weston_subsurface,
3559 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003560 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003561 assert(sub->surface != sub->parent);
3562
3563 if (weston_surface_is_mapped(sub->surface))
3564 weston_surface_unmap(sub->surface);
3565
3566 weston_subsurface_unlink_parent(sub);
3567}
3568
3569static void
3570subsurface_resource_destroy(struct wl_resource *resource)
3571{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003572 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003573
3574 if (sub)
3575 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003576}
3577
3578static void
3579subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3580{
3581 wl_resource_destroy(resource);
3582}
3583
3584static void
3585weston_subsurface_link_parent(struct weston_subsurface *sub,
3586 struct weston_surface *parent)
3587{
3588 sub->parent = parent;
3589 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003590 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003591 &sub->parent_destroy_listener);
3592
3593 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3594 wl_list_insert(&parent->subsurface_list_pending,
3595 &sub->parent_link_pending);
3596}
3597
3598static void
3599weston_subsurface_link_surface(struct weston_subsurface *sub,
3600 struct weston_surface *surface)
3601{
3602 sub->surface = surface;
3603 sub->surface_destroy_listener.notify =
3604 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003605 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003606 &sub->surface_destroy_listener);
3607}
3608
3609static void
3610weston_subsurface_destroy(struct weston_subsurface *sub)
3611{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003612 struct weston_view *view, *next;
3613
Pekka Paalanene67858b2013-04-25 13:57:42 +03003614 assert(sub->surface);
3615
3616 if (sub->resource) {
3617 assert(weston_surface_to_subsurface(sub->surface) == sub);
3618 assert(sub->parent_destroy_listener.notify ==
3619 subsurface_handle_parent_destroy);
3620
George Kiagiadakised04d382014-06-13 18:10:26 +02003621 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3622 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003623 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003624 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003625
Pekka Paalanene67858b2013-04-25 13:57:42 +03003626 if (sub->parent)
3627 weston_subsurface_unlink_parent(sub);
3628
Jason Ekstrand7b982072014-05-20 14:33:03 -05003629 weston_surface_state_fini(&sub->cached);
3630 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003631
3632 sub->surface->configure = NULL;
3633 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003634 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003635 } else {
3636 /* the dummy weston_subsurface for the parent itself */
3637 assert(sub->parent_destroy_listener.notify == NULL);
3638 wl_list_remove(&sub->parent_link);
3639 wl_list_remove(&sub->parent_link_pending);
3640 }
3641
3642 wl_list_remove(&sub->surface_destroy_listener.link);
3643 free(sub);
3644}
3645
3646static const struct wl_subsurface_interface subsurface_implementation = {
3647 subsurface_destroy,
3648 subsurface_set_position,
3649 subsurface_place_above,
3650 subsurface_place_below,
3651 subsurface_set_sync,
3652 subsurface_set_desync
3653};
3654
3655static struct weston_subsurface *
3656weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3657 struct weston_surface *parent)
3658{
3659 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003660 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003661
Bryce Harringtonde16d892014-11-20 22:21:57 -08003662 sub = zalloc(sizeof *sub);
3663 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003664 return NULL;
3665
Jason Ekstranda7af7042013-10-12 22:38:11 -05003666 wl_list_init(&sub->unused_views);
3667
Jason Ekstranda85118c2013-06-27 20:17:02 -05003668 sub->resource =
3669 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003670 if (!sub->resource) {
3671 free(sub);
3672 return NULL;
3673 }
3674
Jason Ekstranda85118c2013-06-27 20:17:02 -05003675 wl_resource_set_implementation(sub->resource,
3676 &subsurface_implementation,
3677 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003678 weston_subsurface_link_surface(sub, surface);
3679 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003680 weston_surface_state_init(&sub->cached);
3681 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003682 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003683
3684 return sub;
3685}
3686
3687/* Create a dummy subsurface for having the parent itself in its
3688 * sub-surface lists. Makes stacking order manipulation easy.
3689 */
3690static struct weston_subsurface *
3691weston_subsurface_create_for_parent(struct weston_surface *parent)
3692{
3693 struct weston_subsurface *sub;
3694
Bryce Harringtonde16d892014-11-20 22:21:57 -08003695 sub = zalloc(sizeof *sub);
3696 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003697 return NULL;
3698
3699 weston_subsurface_link_surface(sub, parent);
3700 sub->parent = parent;
3701 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3702 wl_list_insert(&parent->subsurface_list_pending,
3703 &sub->parent_link_pending);
3704
3705 return sub;
3706}
3707
3708static void
3709subcompositor_get_subsurface(struct wl_client *client,
3710 struct wl_resource *resource,
3711 uint32_t id,
3712 struct wl_resource *surface_resource,
3713 struct wl_resource *parent_resource)
3714{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003715 struct weston_surface *surface =
3716 wl_resource_get_user_data(surface_resource);
3717 struct weston_surface *parent =
3718 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003719 struct weston_subsurface *sub;
3720 static const char where[] = "get_subsurface: wl_subsurface@";
3721
3722 if (surface == parent) {
3723 wl_resource_post_error(resource,
3724 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3725 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003726 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003727 return;
3728 }
3729
3730 if (weston_surface_to_subsurface(surface)) {
3731 wl_resource_post_error(resource,
3732 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3733 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003734 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003735 return;
3736 }
3737
Pekka Paalanen50b67472014-10-01 15:02:41 +03003738 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3739 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003740 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003741
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003742 if (weston_surface_get_main_surface(parent) == surface) {
3743 wl_resource_post_error(resource,
3744 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3745 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003746 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003747 return;
3748 }
3749
Pekka Paalanene67858b2013-04-25 13:57:42 +03003750 /* make sure the parent is in its own list */
3751 if (wl_list_empty(&parent->subsurface_list)) {
3752 if (!weston_subsurface_create_for_parent(parent)) {
3753 wl_resource_post_no_memory(resource);
3754 return;
3755 }
3756 }
3757
3758 sub = weston_subsurface_create(id, surface, parent);
3759 if (!sub) {
3760 wl_resource_post_no_memory(resource);
3761 return;
3762 }
3763
3764 surface->configure = subsurface_configure;
3765 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003766 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003767}
3768
3769static void
3770subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3771{
3772 wl_resource_destroy(resource);
3773}
3774
3775static const struct wl_subcompositor_interface subcompositor_interface = {
3776 subcompositor_destroy,
3777 subcompositor_get_subsurface
3778};
3779
3780static void
3781bind_subcompositor(struct wl_client *client,
3782 void *data, uint32_t version, uint32_t id)
3783{
3784 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003785 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003786
Jason Ekstranda85118c2013-06-27 20:17:02 -05003787 resource =
3788 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003789 if (resource == NULL) {
3790 wl_client_post_no_memory(client);
3791 return;
3792 }
3793 wl_resource_set_implementation(resource, &subcompositor_interface,
3794 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003795}
3796
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003797/** Set a DPMS mode on all of the compositor's outputs
3798 *
3799 * \param compositor The compositor instance
3800 * \param state The DPMS state the outputs will be set to
3801 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003802static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003803weston_compositor_dpms(struct weston_compositor *compositor,
3804 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003805{
3806 struct weston_output *output;
3807
3808 wl_list_for_each(output, &compositor->output_list, link)
3809 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003810 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003811}
3812
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003813/** Restores the compositor to active status
3814 *
3815 * \param compositor The compositor instance
3816 *
3817 * If the compositor was in a sleeping mode, all outputs are powered
3818 * back on via DPMS. Otherwise if the compositor was inactive
3819 * (idle/locked, offscreen, or sleeping) then the compositor's wake
3820 * signal will fire.
3821 *
3822 * Restarts the idle timer.
3823 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003824WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003825weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003826{
Neil Roberts8b62e202013-09-30 13:14:47 +01003827 uint32_t old_state = compositor->state;
3828
3829 /* The state needs to be changed before emitting the wake
3830 * signal because that may try to schedule a repaint which
3831 * will not work if the compositor is still sleeping */
3832 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3833
3834 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003835 case WESTON_COMPOSITOR_SLEEPING:
3836 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3837 /* fall through */
3838 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003839 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003840 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003841 /* fall through */
3842 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003843 wl_event_source_timer_update(compositor->idle_source,
3844 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003845 }
3846}
3847
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003848/** Turns off rendering and frame events for the compositor.
3849 *
3850 * \param compositor The compositor instance
3851 *
3852 * This is used for example to prevent further rendering while the
3853 * compositor is shutting down.
3854 *
3855 * \note When offscreen state is entered, outputs will be powered
3856 * back on if they were sleeping (in DPMS off mode), even though
3857 * no rendering will be performed.
3858 *
3859 * Stops the idle timer.
3860 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003861WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003862weston_compositor_offscreen(struct weston_compositor *compositor)
3863{
3864 switch (compositor->state) {
3865 case WESTON_COMPOSITOR_OFFSCREEN:
3866 return;
3867 case WESTON_COMPOSITOR_SLEEPING:
3868 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3869 /* fall through */
3870 default:
3871 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3872 wl_event_source_timer_update(compositor->idle_source, 0);
3873 }
3874}
3875
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003876/** Powers down all attached output devices
3877 *
3878 * \param compositor The compositor instance
3879 *
3880 * Causes rendering to the outputs to cease, and no frame events to be
3881 * sent. Only powers down the outputs if the compositor is not already
3882 * in sleep mode.
3883 *
3884 * Stops the idle timer.
3885 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003886WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003887weston_compositor_sleep(struct weston_compositor *compositor)
3888{
3889 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3890 return;
3891
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003892 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003893 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3894 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3895}
3896
Bryce Harringtonc9626a32015-12-11 13:11:38 -08003897/** Sets compositor to idle mode
3898 *
3899 * \param data The compositor instance
3900 *
3901 * This is called when the idle timer fires. Once the compositor is in
3902 * idle mode it requires a wake action (e.g. via
3903 * weston_compositor_wake()) to restore it. The compositor's
3904 * idle_signal will be triggered when the idle event occurs.
3905 *
3906 * Idleness can be inhibited by setting the compositor's idle_inhibit
3907 * property.
3908 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003909static int
3910idle_handler(void *data)
3911{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003912 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003913
3914 if (compositor->idle_inhibit)
3915 return 1;
3916
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003917 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003918 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003919
3920 return 1;
3921}
3922
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003923WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003924weston_plane_init(struct weston_plane *plane,
3925 struct weston_compositor *ec,
3926 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003927{
3928 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003929 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003930 plane->x = x;
3931 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003932 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003933
3934 /* Init the link so that the call to wl_list_remove() when releasing
3935 * the plane without ever stacking doesn't lead to a crash */
3936 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003937}
3938
3939WL_EXPORT void
3940weston_plane_release(struct weston_plane *plane)
3941{
Xiong Zhang97116532013-10-23 13:58:31 +08003942 struct weston_view *view;
3943
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003944 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003945 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003946
Xiong Zhang97116532013-10-23 13:58:31 +08003947 wl_list_for_each(view, &plane->compositor->view_list, link) {
3948 if (view->plane == plane)
3949 view->plane = NULL;
3950 }
3951
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003952 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003953}
3954
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003955WL_EXPORT void
3956weston_compositor_stack_plane(struct weston_compositor *ec,
3957 struct weston_plane *plane,
3958 struct weston_plane *above)
3959{
3960 if (above)
3961 wl_list_insert(above->link.prev, &plane->link);
3962 else
3963 wl_list_insert(&ec->plane_list, &plane->link);
3964}
3965
Casey Dahlin9074db52012-04-19 22:50:09 -04003966static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003967{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003968 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003969}
3970
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003971static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003972bind_output(struct wl_client *client,
3973 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003974{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003975 struct weston_output *output = data;
3976 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003977 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003978
Jason Ekstranda85118c2013-06-27 20:17:02 -05003979 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06003980 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003981 if (resource == NULL) {
3982 wl_client_post_no_memory(client);
3983 return;
3984 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003985
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003986 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003987 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003988
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003989 wl_output_send_geometry(resource,
3990 output->x,
3991 output->y,
3992 output->mm_width,
3993 output->mm_height,
3994 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003995 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003996 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003997 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003998 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003999 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004000
4001 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004002 wl_output_send_mode(resource,
4003 mode->flags,
4004 mode->width,
4005 mode->height,
4006 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004007 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004008
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004009 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004010 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004011}
4012
David Fort0de859e2016-05-27 23:22:57 +02004013/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004014static void
David Fort0de859e2016-05-27 23:22:57 +02004015weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4016 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004017{
4018 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004019 bool start_resizing = false;
4020
4021 if (!delta_width)
4022 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004023
4024 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004025 if (output == resized_output) {
4026 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004027 continue;
4028 }
4029
David Fort0de859e2016-05-27 23:22:57 +02004030 if (start_resizing) {
4031 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004032 output->dirty = 1;
4033 }
4034 }
4035}
4036
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004037WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004038weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004039{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004040 struct wl_resource *resource;
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004041 struct weston_view *view;
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004042
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02004043 output->destroying = 1;
4044
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004045 wl_list_for_each(view, &output->compositor->view_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -08004046 if (view->output_mask & (1u << output->id))
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03004047 weston_view_assign_output(view);
4048 }
4049
Pekka Paalanen0513a952014-05-21 16:17:27 +03004050 wl_event_source_remove(output->repaint_timer);
4051
Pekka Paalanen133e4392014-09-23 22:08:46 -04004052 weston_presentation_feedback_discard_list(&output->feedback_list);
4053
David Fort0de859e2016-05-27 23:22:57 +02004054 weston_compositor_reflow_outputs(output->compositor, output, output->width);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02004055 wl_list_remove(&output->link);
4056
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004057 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01004058 wl_signal_emit(&output->destroy_signal, output);
4059
Richard Hughesafe690c2013-05-02 10:10:04 +01004060 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04004061 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004062 pixman_region32_fini(&output->previous_damage);
Bryce Harrington89324ce2015-12-23 18:38:07 -08004063 output->compositor->output_id_pool &= ~(1u << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004064
Giulio Camuffo00535ce2014-09-06 16:18:02 +03004065 wl_resource_for_each(resource, &output->resource_list) {
4066 wl_resource_set_destructor(resource, NULL);
4067 }
4068
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004069 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004070}
4071
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004072WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004073weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004074{
Scott Moreau850ca422012-05-21 15:21:25 -06004075 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004076
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004077 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004078 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004079
Scott Moreauccbf29d2012-02-22 14:21:41 -07004080 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004081 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004082 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004083 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004084 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004085 weston_matrix_scale(&output->matrix, magnification,
4086 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004087 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004088
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004089 switch (output->transform) {
4090 case WL_OUTPUT_TRANSFORM_FLIPPED:
4091 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4092 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4093 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4094 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4095 weston_matrix_scale(&output->matrix, -1, 1, 1);
4096 break;
4097 }
4098
4099 switch (output->transform) {
4100 default:
4101 case WL_OUTPUT_TRANSFORM_NORMAL:
4102 case WL_OUTPUT_TRANSFORM_FLIPPED:
4103 break;
4104 case WL_OUTPUT_TRANSFORM_90:
4105 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4106 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4107 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4108 break;
4109 case WL_OUTPUT_TRANSFORM_180:
4110 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4111 weston_matrix_translate(&output->matrix,
4112 -output->width, -output->height, 0);
4113 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4114 break;
4115 case WL_OUTPUT_TRANSFORM_270:
4116 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4117 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4118 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4119 break;
4120 }
4121
4122 if (output->current_scale != 1)
4123 weston_matrix_scale(&output->matrix,
4124 output->current_scale,
4125 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004126
Scott Moreauccbf29d2012-02-22 14:21:41 -07004127 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004128
4129 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004130}
4131
Scott Moreau1bad5db2012-08-18 01:04:05 -06004132static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004133weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004134{
4135 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004136 output->native_scale = scale;
4137 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004138
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004139 convert_size_by_transform_scale(&output->width, &output->height,
4140 output->current_mode->width,
4141 output->current_mode->height,
4142 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004143}
4144
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004145static void
4146weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004147{
4148 output->x = x;
4149 output->y = y;
4150
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004151 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004152 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004153 output->width,
4154 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004155}
4156
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004157WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004158weston_output_move(struct weston_output *output, int x, int y)
4159{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004160 struct wl_resource *resource;
4161
4162 output->move_x = x - output->x;
4163 output->move_y = y - output->y;
4164
4165 if (output->move_x == 0 && output->move_y == 0)
4166 return;
4167
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004168 weston_output_init_geometry(output, x, y);
4169
4170 output->dirty = 1;
4171
4172 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004173 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004174
4175 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004176 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004177 wl_output_send_geometry(resource,
4178 output->x,
4179 output->y,
4180 output->mm_width,
4181 output->mm_height,
4182 output->subpixel,
4183 output->make,
4184 output->model,
4185 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004186
FORT David8a120692016-04-26 23:34:06 +02004187 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004188 wl_output_send_done(resource);
4189 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004190}
4191
Bryce Harrington3f650b82015-12-23 11:01:58 -08004192/** Initialize a weston_output object's parameters
4193 *
4194 * \param output The weston_output object to initialize
4195 * \param c The output's compositor
4196 * \param x x coordinate for the output in global coordinate space
4197 * \param y y coordinate for the output in global coordinate space
4198 * \param mm_width Physical width of the output as reported by the backend
4199 * \param mm_height Physical height of the output as reported by the backend
4200 * \param transform Rotation of the output
4201 * \param scale Native scaling factor for the output
4202 *
4203 * Sets up the transformation, zoom, and geometry of the output using
4204 * the input properties.
4205 *
4206 * Establishes a repaint timer for the output with the relevant display
4207 * object's event loop. See output_repaint_timer_handler().
4208 *
4209 * The output is assigned an ID. Weston can support up to 32 distinct
4210 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4211 * is referred to and used to find the first available ID number, and
4212 * then this ID is marked as used in output_id_pool.
4213 *
4214 * The output is also assigned a Wayland global with the wl_output
4215 * external interface.
4216 */
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004217WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004218weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004219 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004220 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004221{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004222 struct wl_event_loop *loop;
4223
Bryce Harrington18e45732015-12-23 20:53:53 -08004224 /* Verify we haven't reached the limit of 32 available output IDs */
4225 assert(ffs(~c->output_id_pool) > 0);
4226
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004227 output->compositor = c;
4228 output->x = x;
4229 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004230 output->mm_width = mm_width;
4231 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004232 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004233 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004234
Alexander Larsson0b135062013-05-28 16:23:36 +02004235 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004236 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004237
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004238 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004239 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004240
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004241 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004242 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004243 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004244 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004245 wl_list_init(&output->feedback_list);
Dawid Gajownik2f7d33d2015-08-06 21:04:16 -03004246 wl_list_init(&output->link);
Benjamin Franzke06286262011-05-06 19:12:33 +02004247
Pekka Paalanen0513a952014-05-21 16:17:27 +03004248 loop = wl_display_get_event_loop(c->wl_display);
4249 output->repaint_timer = wl_event_loop_add_timer(loop,
4250 output_repaint_timer_handler, output);
4251
Bryce Harrington3f650b82015-12-23 11:01:58 -08004252 /* Invert the output id pool and look for the lowest numbered
4253 * switch (the least significant bit). Take that bit's position
4254 * as our ID, and mark it used in the compositor's output_id_pool.
4255 */
Casey Dahlin58ba1372012-04-19 22:50:08 -04004256 output->id = ffs(~output->compositor->output_id_pool) - 1;
Bryce Harrington89324ce2015-12-23 18:38:07 -08004257 output->compositor->output_id_pool |= 1u << output->id;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004258
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004259 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004260 wl_global_create(c->wl_display, &wl_output_interface, 2,
4261 output, bind_output);
Giulio Camuffob1147152015-05-06 21:41:57 +03004262}
4263
4264/** Adds an output to the compositor's output list and
4265 * send the compositor's output_created signal.
4266 *
4267 * \param compositor The compositor instance.
4268 * \param output The output to be added.
4269 */
4270WL_EXPORT void
4271weston_compositor_add_output(struct weston_compositor *compositor,
4272 struct weston_output *output)
4273{
4274 wl_list_insert(compositor->output_list.prev, &output->link);
4275 wl_signal_emit(&compositor->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004276}
4277
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004278WL_EXPORT void
4279weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004280 double device_x, double device_y,
4281 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004282{
Derek Foreman0f679412014-10-02 13:41:17 -05004283 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004284 device_x,
4285 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004286 0.0,
4287 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004288
Derek Foreman67a18b92015-03-24 11:36:14 -05004289 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004290
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004291 *x = p.f[0] / p.f[3];
4292 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004293}
4294
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004295static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004296destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004297{
Jonny Lamb74130762013-11-26 18:19:46 +01004298 struct weston_surface *surface =
4299 wl_resource_get_user_data(resource);
4300
Pekka Paalanen4826f872016-04-22 14:14:38 +03004301 if (!surface)
4302 return;
4303
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004304 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004305 surface->pending.buffer_viewport.buffer.src_width =
4306 wl_fixed_from_int(-1);
4307 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004308 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004309}
4310
4311static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004312viewport_destroy(struct wl_client *client,
4313 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004314{
4315 wl_resource_destroy(resource);
4316}
4317
4318static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004319viewport_set_source(struct wl_client *client,
4320 struct wl_resource *resource,
4321 wl_fixed_t src_x,
4322 wl_fixed_t src_y,
4323 wl_fixed_t src_width,
4324 wl_fixed_t src_height)
4325{
4326 struct weston_surface *surface =
4327 wl_resource_get_user_data(resource);
4328
Pekka Paalanen4826f872016-04-22 14:14:38 +03004329 if (!surface) {
4330 wl_resource_post_error(resource,
4331 WP_VIEWPORT_ERROR_NO_SURFACE,
4332 "wl_surface for this viewport is no longer exists");
4333 return;
4334 }
4335
4336 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004337
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004338 if (src_width == wl_fixed_from_int(-1) &&
4339 src_height == wl_fixed_from_int(-1)) {
4340 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004341 surface->pending.buffer_viewport.buffer.src_width =
4342 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004343 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004344 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004345 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004346
4347 if (src_width <= 0 || src_height <= 0) {
4348 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004349 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004350 "source size must be positive (%fx%f)",
4351 wl_fixed_to_double(src_width),
4352 wl_fixed_to_double(src_height));
4353 return;
4354 }
4355
4356 surface->pending.buffer_viewport.buffer.src_x = src_x;
4357 surface->pending.buffer_viewport.buffer.src_y = src_y;
4358 surface->pending.buffer_viewport.buffer.src_width = src_width;
4359 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004360 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004361}
4362
4363static void
4364viewport_set_destination(struct wl_client *client,
4365 struct wl_resource *resource,
4366 int32_t dst_width,
4367 int32_t dst_height)
4368{
4369 struct weston_surface *surface =
4370 wl_resource_get_user_data(resource);
4371
Pekka Paalanen4826f872016-04-22 14:14:38 +03004372 if (!surface) {
4373 wl_resource_post_error(resource,
4374 WP_VIEWPORT_ERROR_NO_SURFACE,
4375 "wl_surface for this viewport no longer exists");
4376 return;
4377 }
4378
4379 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004380
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004381 if (dst_width == -1 && dst_height == -1) {
4382 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004383 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004384 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004385 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004386 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004387
4388 if (dst_width <= 0 || dst_height <= 0) {
4389 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004390 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004391 "destination size must be positive (%dx%d)",
4392 dst_width, dst_height);
4393 return;
4394 }
4395
4396 surface->pending.buffer_viewport.surface.width = dst_width;
4397 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004398 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004399}
4400
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004401static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004402 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004403 viewport_set_source,
4404 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004405};
4406
4407static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004408viewporter_destroy(struct wl_client *client,
4409 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004410{
4411 wl_resource_destroy(resource);
4412}
4413
4414static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004415viewporter_get_viewport(struct wl_client *client,
4416 struct wl_resource *viewporter,
4417 uint32_t id,
4418 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004419{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004420 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004421 struct weston_surface *surface =
4422 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004423 struct wl_resource *resource;
4424
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004425 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004426 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004427 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004428 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004429 return;
4430 }
4431
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004432 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004433 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004434 if (resource == NULL) {
4435 wl_client_post_no_memory(client);
4436 return;
4437 }
4438
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004439 wl_resource_set_implementation(resource, &viewport_interface,
4440 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004441
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004442 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004443}
4444
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004445static const struct wp_viewporter_interface viewporter_interface = {
4446 viewporter_destroy,
4447 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004448};
4449
4450static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004451bind_viewporter(struct wl_client *client,
4452 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004453{
4454 struct wl_resource *resource;
4455
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004456 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004457 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004458 if (resource == NULL) {
4459 wl_client_post_no_memory(client);
4460 return;
4461 }
4462
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004463 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004464 NULL, NULL);
4465}
4466
4467static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004468destroy_presentation_feedback(struct wl_resource *feedback_resource)
4469{
4470 struct weston_presentation_feedback *feedback;
4471
4472 feedback = wl_resource_get_user_data(feedback_resource);
4473
4474 wl_list_remove(&feedback->link);
4475 free(feedback);
4476}
4477
4478static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004479presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4480{
4481 wl_resource_destroy(resource);
4482}
4483
4484static void
4485presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004486 struct wl_resource *presentation_resource,
4487 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004488 uint32_t callback)
4489{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004490 struct weston_surface *surface;
4491 struct weston_presentation_feedback *feedback;
4492
4493 surface = wl_resource_get_user_data(surface_resource);
4494
Bryce Harringtonde16d892014-11-20 22:21:57 -08004495 feedback = zalloc(sizeof *feedback);
4496 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004497 goto err_calloc;
4498
4499 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004500 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004501 1, callback);
4502 if (!feedback->resource)
4503 goto err_create;
4504
4505 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4506 destroy_presentation_feedback);
4507
4508 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4509
4510 return;
4511
4512err_create:
4513 free(feedback);
4514
4515err_calloc:
4516 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004517}
4518
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004519static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004520 presentation_destroy,
4521 presentation_feedback
4522};
4523
4524static void
4525bind_presentation(struct wl_client *client,
4526 void *data, uint32_t version, uint32_t id)
4527{
4528 struct weston_compositor *compositor = data;
4529 struct wl_resource *resource;
4530
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004531 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004532 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004533 if (resource == NULL) {
4534 wl_client_post_no_memory(client);
4535 return;
4536 }
4537
4538 wl_resource_set_implementation(resource, &presentation_implementation,
4539 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004540 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004541}
4542
4543static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004544compositor_bind(struct wl_client *client,
4545 void *data, uint32_t version, uint32_t id)
4546{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004547 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004548 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004549
Jason Ekstranda85118c2013-06-27 20:17:02 -05004550 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004551 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004552 if (resource == NULL) {
4553 wl_client_post_no_memory(client);
4554 return;
4555 }
4556
4557 wl_resource_set_implementation(resource, &compositor_interface,
4558 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004559}
4560
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004561WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004562weston_environment_get_fd(const char *env)
4563{
4564 char *e, *end;
4565 int fd, flags;
4566
4567 e = getenv(env);
4568 if (!e)
4569 return -1;
4570 fd = strtol(e, &end, 0);
4571 if (*end != '\0')
4572 return -1;
4573
4574 flags = fcntl(fd, F_GETFD);
4575 if (flags == -1)
4576 return -1;
4577
4578 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4579 unsetenv(env);
4580
4581 return fd;
4582}
4583
Pekka Paalanenb5026542014-11-12 15:09:24 +02004584static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004585timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02004586 uint32_t key, void *data)
4587{
4588 struct weston_compositor *compositor = data;
4589
4590 if (weston_timeline_enabled_)
4591 weston_timeline_close();
4592 else
4593 weston_timeline_open(compositor);
4594}
4595
Giulio Camuffo459137b2014-10-11 23:56:24 +03004596/** Create the compositor.
4597 *
4598 * This functions creates and initializes a compositor instance.
4599 *
4600 * \param display The Wayland display to be used.
4601 * \param user_data A pointer to an object that can later be retrieved
4602 * using the \ref weston_compositor_get_user_data function.
4603 * \return The compositor instance on success or NULL on failure.
4604 */
4605WL_EXPORT struct weston_compositor *
4606weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004607{
Giulio Camuffo459137b2014-10-11 23:56:24 +03004608 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004609 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004610
Giulio Camuffo459137b2014-10-11 23:56:24 +03004611 ec = zalloc(sizeof *ec);
4612 if (!ec)
4613 return NULL;
4614
4615 ec->wl_display = display;
4616 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004617 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004618 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004619 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004620 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004621 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004622 wl_signal_init(&ec->idle_signal);
4623 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004624 wl_signal_init(&ec->show_input_panel_signal);
4625 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004626 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004627 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004628 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004629 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004630 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02004631 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004632 wl_signal_init(&ec->session_signal);
4633 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004634
Casey Dahlin58ba1372012-04-19 22:50:08 -04004635 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03004636 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04004637
Derek Foreman152254b2015-11-26 14:17:48 -06004638 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004639 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004640 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004641
Giulio Camuffo954f1832014-10-11 18:27:30 +03004642 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004643 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004644 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004645
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004646 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03004647 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004648 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004649
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02004650 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004651 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03004652 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004653
Jason Ekstranda7af7042013-10-12 22:38:11 -05004654 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004655 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004656 wl_list_init(&ec->layer_list);
4657 wl_list_init(&ec->seat_list);
4658 wl_list_init(&ec->output_list);
4659 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004660 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004661 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004662 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004663 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004664 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004665
Xiong Zhang97116532013-10-23 13:58:31 +08004666 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004667 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004668
Giulio Camuffo459137b2014-10-11 23:56:24 +03004669 wl_data_device_manager_init(ec->wl_display);
4670
4671 wl_display_init_shm(ec->wl_display);
4672
4673 loop = wl_display_get_event_loop(ec->wl_display);
4674 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004675
Giulio Camuffo459137b2014-10-11 23:56:24 +03004676 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4677 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4678
4679 weston_compositor_add_debug_binding(ec, KEY_T,
4680 timeline_key_binding_handler, ec);
4681
Giulio Camuffo459137b2014-10-11 23:56:24 +03004682 return ec;
4683
4684fail:
4685 free(ec);
4686 return NULL;
4687}
4688
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004689WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004690weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004691{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004692 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004693
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004694 wl_event_source_remove(ec->idle_source);
4695
Matt Roper361d2ad2011-08-29 13:52:23 -07004696 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004697 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004698 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004699
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004700 if (ec->renderer)
4701 ec->renderer->destroy(ec);
4702
Daniel Stone325fc2d2012-05-30 16:31:58 +01004703 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09004704 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004705 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004706 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004707 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004708 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004709
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004710 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07004711}
4712
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004713WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004714weston_compositor_exit_with_code(struct weston_compositor *compositor,
4715 int exit_code)
4716{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004717 if (compositor->exit_code == EXIT_SUCCESS)
4718 compositor->exit_code = exit_code;
4719
Giulio Camuffo459137b2014-10-11 23:56:24 +03004720 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04004721}
4722
4723WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004724weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4725 const struct weston_pointer_grab_interface *interface)
4726{
4727 struct weston_seat *seat;
4728
4729 ec->default_pointer_grab = interface;
4730 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004731 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4732
4733 if (pointer)
4734 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004735 }
4736}
4737
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004738WL_EXPORT int
4739weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4740 clockid_t clk_id)
4741{
4742 struct timespec ts;
4743
4744 if (clock_gettime(clk_id, &ts) < 0)
4745 return -1;
4746
4747 compositor->presentation_clock = clk_id;
4748
4749 return 0;
4750}
4751
4752/*
4753 * For choosing the software clock, when the display hardware or API
4754 * does not expose a compatible presentation timestamp.
4755 */
4756WL_EXPORT int
4757weston_compositor_set_presentation_clock_software(
4758 struct weston_compositor *compositor)
4759{
4760 /* In order of preference */
4761 static const clockid_t clocks[] = {
4762 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4763 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4764 CLOCK_MONOTONIC, /* no jumps, may crawl */
4765 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4766 CLOCK_REALTIME /* may jump and crawl */
4767 };
4768 unsigned i;
4769
4770 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4771 if (weston_compositor_set_presentation_clock(compositor,
4772 clocks[i]) == 0)
4773 return 0;
4774
4775 weston_log("Error: no suitable presentation clock available.\n");
4776
4777 return -1;
4778}
4779
Pekka Paalanen662f3842015-03-18 12:17:26 +02004780/** Read the current time from the Presentation clock
4781 *
4782 * \param compositor
4783 * \param ts[out] The current time.
4784 *
4785 * \note Reading the current time in user space is always imprecise to some
4786 * degree.
4787 *
4788 * This function is never meant to fail. If reading the clock does fail,
4789 * an error message is logged and a zero time is returned. Callers are not
4790 * supposed to detect or react to failures.
4791 */
4792WL_EXPORT void
4793weston_compositor_read_presentation_clock(
4794 const struct weston_compositor *compositor,
4795 struct timespec *ts)
4796{
4797 static bool warned;
4798 int ret;
4799
4800 ret = clock_gettime(compositor->presentation_clock, ts);
4801 if (ret < 0) {
4802 ts->tv_sec = 0;
4803 ts->tv_nsec = 0;
4804
4805 if (!warned)
4806 weston_log("Error: failure to read "
4807 "the presentation clock %#x: '%m' (%d)\n",
4808 compositor->presentation_clock, errno);
4809 warned = true;
4810 }
4811}
4812
Pekka Paalanen230f3b12014-09-29 14:18:40 -04004813/** Import dmabuf buffer into current renderer
4814 *
4815 * \param compositor
4816 * \param buffer the dmabuf buffer to import
4817 * \return true on usable buffers, false otherwise
4818 *
4819 * This function tests that the linux_dmabuf_buffer is usable
4820 * for the current renderer. Returns false on unusable buffers. Usually
4821 * usability is tested by importing the dmabufs for composition.
4822 *
4823 * This hook is also used for detecting if the renderer supports
4824 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
4825 * supported.
4826 * */
4827WL_EXPORT bool
4828weston_compositor_import_dmabuf(struct weston_compositor *compositor,
4829 struct linux_dmabuf_buffer *buffer)
4830{
4831 struct weston_renderer *renderer;
4832
4833 renderer = compositor->renderer;
4834
4835 if (renderer->import_dmabuf == NULL)
4836 return false;
4837
4838 return renderer->import_dmabuf(compositor, buffer);
4839}
4840
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004841WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004842weston_version(int *major, int *minor, int *micro)
4843{
4844 *major = WESTON_VERSION_MAJOR;
4845 *minor = WESTON_VERSION_MINOR;
4846 *micro = WESTON_VERSION_MICRO;
4847}
4848
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004849WL_EXPORT void *
4850weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004851{
Derek Foreman3f86e502015-06-08 11:46:54 -05004852 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004853 char path[PATH_MAX];
4854 void *module, *init;
4855
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004856 if (name == NULL)
4857 return NULL;
4858
Derek Foreman3f86e502015-06-08 11:46:54 -05004859 if (name[0] != '/') {
4860 if (builddir)
4861 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
4862 else
Giulio Camuffo179fcda2016-06-02 21:48:14 +03004863 snprintf(path, sizeof path, "%s/%s", LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004864 } else {
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004865 snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05004866 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004867
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004868 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4869 if (module) {
4870 weston_log("Module '%s' already loaded\n", path);
4871 dlclose(module);
4872 return NULL;
4873 }
4874
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004875 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004876 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004877 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004878 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004879 return NULL;
4880 }
4881
4882 init = dlsym(module, entrypoint);
4883 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004884 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004885 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004886 return NULL;
4887 }
4888
4889 return init;
4890}
4891
Giulio Camuffo459137b2014-10-11 23:56:24 +03004892
4893/** Destroys the compositor.
4894 *
4895 * This function cleans up the compositor state and destroys it.
4896 *
4897 * \param compositor The compositor to be destroyed.
4898 */
4899WL_EXPORT void
4900weston_compositor_destroy(struct weston_compositor *compositor)
4901{
4902 /* prevent further rendering while shutting down */
4903 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4904
4905 wl_signal_emit(&compositor->destroy_signal, compositor);
4906
4907 weston_compositor_xkb_destroy(compositor);
4908
Giulio Camuffo2d24e642015-10-03 16:25:15 +03004909 if (compositor->backend)
4910 compositor->backend->destroy(compositor);
Giulio Camuffo459137b2014-10-11 23:56:24 +03004911 free(compositor);
4912}
4913
4914/** Instruct the compositor to exit.
4915 *
4916 * This functions does not directly destroy the compositor object, it merely
4917 * command it to start the tear down process. It is not guaranteed that the
4918 * tear down will happen immediately.
4919 *
4920 * \param compositor The compositor to tear down.
4921 */
4922WL_EXPORT void
4923weston_compositor_exit(struct weston_compositor *compositor)
4924{
4925 compositor->exit(compositor);
4926}
4927
4928/** Return the user data stored in the compositor.
4929 *
4930 * This function returns the user data pointer set with user_data parameter
4931 * to the \ref weston_compositor_create function.
4932 */
4933WL_EXPORT void *
4934weston_compositor_get_user_data(struct weston_compositor *compositor)
4935{
4936 return compositor->user_data;
4937}
Pekka Paalanendd186732016-06-03 14:49:54 +03004938
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004939static const char * const backend_map[] = {
4940 [WESTON_BACKEND_DRM] = "drm-backend.so",
4941 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
4942 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
4943 [WESTON_BACKEND_RDP] = "rdp-backend.so",
4944 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
4945 [WESTON_BACKEND_X11] = "x11-backend.so",
4946};
4947
Pekka Paalanendd186732016-06-03 14:49:54 +03004948/** Load a backend into a weston_compositor
4949 *
4950 * A backend must be loaded to make a weston_compositor work. A backend
4951 * provides input and output capabilities, and determines the renderer to use.
4952 *
4953 * \param compositor A compositor that has not had a backend loaded yet.
4954 * \param backend Name of the backend file.
4955 * \param config_base A pointer to a backend-specific configuration
4956 * structure's 'base' member.
4957 *
4958 * \return 0 on success, or -1 on error.
4959 */
4960WL_EXPORT int
4961weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004962 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03004963 struct weston_backend_config *config_base)
4964{
4965 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03004966 struct weston_backend_config *config_base);
4967
Pekka Paalanen50dbf382016-06-03 15:23:46 +03004968 if (backend < 0 || backend >= ARRAY_LENGTH(backend_map))
4969 return -1;
4970
4971 backend_init = weston_load_module(backend_map[backend], "backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03004972 if (!backend_init)
4973 return -1;
4974
Pekka Paalanena256c5e2016-06-03 14:56:18 +03004975 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03004976}