blob: c66193a453b0d49633da8f9a7225afad2dda1124 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07006 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050013 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070014 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050026 */
27
Kristian Høgsberga9410222011-01-14 17:22:35 -050028#include "config.h"
29
Daniel Stoneb7452fe2012-06-01 12:14:06 +010030#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010035#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050036#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020037#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010039#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040040#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020041#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020042#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020043#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050045#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040046#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040047#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040048#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050049#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040050#include <sys/time.h>
51#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030052#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050053
Pekka Paalanenb5026542014-11-12 15:09:24 +020054#include "timeline.h"
55
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Pekka Paalanene95ad5c2016-04-15 14:47:08 +030057#include "viewporter-server-protocol.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020058#include "presentation-time-server-protocol.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070059#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070060#include "shared/os-compatibility.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070061#include "shared/string-helpers.h"
Pekka Paalanenaa21f622015-07-03 15:44:50 +030062#include "shared/timespec-util.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040063#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050064#include "version.h"
Pekka Paalanen827b5d22016-06-29 11:54:26 +020065#include "plugin-registry.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050066
Pekka Paalanen0513a952014-05-21 16:17:27 +030067#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
68
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020069static void
Alexander Larsson0b135062013-05-28 16:23:36 +020070weston_output_transform_scale_init(struct weston_output *output,
71 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020072
Rob Bradford27b17932013-06-26 18:08:46 +010073static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050074weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010075
Derek Foreman6ae7bc92014-11-04 10:47:33 -060076static void weston_mode_switch_finish(struct weston_output *output,
77 int mode_changed,
78 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +080079{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020080 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +020081 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020082 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -060083 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +020084
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020085 pixman_region32_init(&old_output_region);
86 pixman_region32_copy(&old_output_region, &output->region);
87
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020088 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +020089 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020090
91 pixman_region32_init(&output->previous_damage);
92 pixman_region32_init_rect(&output->region, output->x, output->y,
93 output->width, output->height);
94
95 weston_output_update_matrix(output);
96
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020097 /* If a pointer falls outside the outputs new geometry, move it to its
98 * lower-right corner */
99 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500100 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200101 int32_t x, y;
102
103 if (!pointer)
104 continue;
105
106 x = wl_fixed_to_int(pointer->x);
107 y = wl_fixed_to_int(pointer->y);
108
109 if (!pixman_region32_contains_point(&old_output_region,
110 x, y, NULL) ||
111 pixman_region32_contains_point(&output->region,
112 x, y, NULL))
113 continue;
114
115 if (x >= output->x + output->width)
116 x = output->x + output->width - 1;
117 if (y >= output->y + output->height)
118 y = output->y + output->height - 1;
119
120 pointer->x = wl_fixed_from_int(x);
121 pointer->y = wl_fixed_from_int(y);
122 }
123
124 pixman_region32_fini(&old_output_region);
125
Derek Foremandd4cd332014-11-10 10:29:59 -0600126 if (!mode_changed && !scale_changed)
127 return;
128
Hardening57388e42013-09-18 23:56:36 +0200129 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600130 wl_resource_for_each(resource, &output->resource_list) {
131 if (mode_changed) {
132 wl_output_send_mode(resource,
133 output->current_mode->flags,
134 output->current_mode->width,
135 output->current_mode->height,
136 output->current_mode->refresh);
137 }
Hardening57388e42013-09-18 23:56:36 +0200138
Derek Foreman41bdc272014-11-05 13:26:57 -0600139 version = wl_resource_get_version(resource);
140 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600141 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200142
Derek Foreman41bdc272014-11-05 13:26:57 -0600143 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
144 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600145 }
146}
147
David Fort0de859e2016-05-27 23:22:57 +0200148
149static void
150weston_compositor_reflow_outputs(struct weston_compositor *compositor,
151 struct weston_output *resized_output, int delta_width);
152
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600153WL_EXPORT int
154weston_output_mode_set_native(struct weston_output *output,
155 struct weston_mode *mode,
156 int32_t scale)
157{
158 int ret;
159 int mode_changed = 0, scale_changed = 0;
David Fort0de859e2016-05-27 23:22:57 +0200160 int32_t old_width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600161
162 if (!output->switch_mode)
163 return -1;
164
165 if (!output->original_mode) {
166 mode_changed = 1;
167 ret = output->switch_mode(output, mode);
168 if (ret < 0)
169 return ret;
170 if (output->current_scale != scale) {
171 scale_changed = 1;
172 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200173 }
174 }
175
David Fort0de859e2016-05-27 23:22:57 +0200176 old_width = output->width;
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600177 output->native_mode = mode;
178 output->native_scale = scale;
179
180 weston_mode_switch_finish(output, mode_changed, scale_changed);
181
David Fort0de859e2016-05-27 23:22:57 +0200182 if (mode_changed || scale_changed) {
183 weston_compositor_reflow_outputs(output->compositor, output, output->width - old_width);
184
185 wl_signal_emit(&output->compositor->output_resized_signal, output);
186 }
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600187 return 0;
188}
189
190WL_EXPORT int
191weston_output_mode_switch_to_native(struct weston_output *output)
192{
193 int ret;
194 int mode_changed = 0, scale_changed = 0;
195
196 if (!output->switch_mode)
197 return -1;
198
199 if (!output->original_mode) {
200 weston_log("already in the native mode\n");
201 return -1;
202 }
203 /* the non fullscreen clients haven't seen a mode set since we
204 * switched into a temporary, so we need to notify them if the
205 * mode at that time is different from the native mode now.
206 */
207 mode_changed = (output->original_mode != output->native_mode);
208 scale_changed = (output->original_scale != output->native_scale);
209
210 ret = output->switch_mode(output, output->native_mode);
211 if (ret < 0)
212 return ret;
213
214 output->current_scale = output->native_scale;
215
216 output->original_mode = NULL;
217 output->original_scale = 0;
218
219 weston_mode_switch_finish(output, mode_changed, scale_changed);
220
221 return 0;
222}
223
224WL_EXPORT int
225weston_output_mode_switch_to_temporary(struct weston_output *output,
226 struct weston_mode *mode,
227 int32_t scale)
228{
229 int ret;
230
231 if (!output->switch_mode)
232 return -1;
233
234 /* original_mode is the last mode non full screen clients have seen,
235 * so we shouldn't change it if we already have one set.
236 */
237 if (!output->original_mode) {
238 output->original_mode = output->native_mode;
239 output->original_scale = output->native_scale;
240 }
241 ret = output->switch_mode(output, mode);
242 if (ret < 0)
243 return ret;
244
245 output->current_scale = scale;
246
247 weston_mode_switch_finish(output, 0, 0);
248
249 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800250}
251
Benjamin Franzke06286262011-05-06 19:12:33 +0200252static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300253region_init_infinite(pixman_region32_t *region)
254{
255 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
256 UINT32_MAX, UINT32_MAX);
257}
258
Pekka Paalanene67858b2013-04-25 13:57:42 +0300259static struct weston_subsurface *
260weston_surface_to_subsurface(struct weston_surface *surface);
261
Jason Ekstranda7af7042013-10-12 22:38:11 -0500262WL_EXPORT struct weston_view *
263weston_view_create(struct weston_surface *surface)
264{
265 struct weston_view *view;
266
Bryce Harringtonde16d892014-11-20 22:21:57 -0800267 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500268 if (view == NULL)
269 return NULL;
270
271 view->surface = surface;
Daniel Stonefb4869d2016-12-09 16:27:54 +0000272 view->plane = &surface->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500273
Jason Ekstranda7af7042013-10-12 22:38:11 -0500274 /* Assign to surface */
275 wl_list_insert(&surface->views, &view->surface_link);
276
277 wl_signal_init(&view->destroy_signal);
278 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300279 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500280
Jason Ekstranda7af7042013-10-12 22:38:11 -0500281 pixman_region32_init(&view->clip);
282
283 view->alpha = 1.0;
284 pixman_region32_init(&view->transform.opaque);
285
286 wl_list_init(&view->geometry.transformation_list);
287 wl_list_insert(&view->geometry.transformation_list,
288 &view->transform.position.link);
289 weston_matrix_init(&view->transform.position.matrix);
290 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200291 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292 pixman_region32_init(&view->transform.boundingbox);
293 view->transform.dirty = 1;
294
Jason Ekstranda7af7042013-10-12 22:38:11 -0500295 return view;
296}
297
Jason Ekstrand108865d2014-06-26 10:04:49 -0700298struct weston_frame_callback {
299 struct wl_resource *resource;
300 struct wl_list link;
301};
302
Pekka Paalanen133e4392014-09-23 22:08:46 -0400303struct weston_presentation_feedback {
304 struct wl_resource *resource;
305
306 /* XXX: could use just wl_resource_get_link() instead */
307 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200308
309 /* The per-surface feedback flags */
310 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400311};
312
313static void
314weston_presentation_feedback_discard(
315 struct weston_presentation_feedback *feedback)
316{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200317 wp_presentation_feedback_send_discarded(feedback->resource);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400318 wl_resource_destroy(feedback->resource);
319}
320
321static void
322weston_presentation_feedback_discard_list(struct wl_list *list)
323{
324 struct weston_presentation_feedback *feedback, *tmp;
325
326 wl_list_for_each_safe(feedback, tmp, list, link)
327 weston_presentation_feedback_discard(feedback);
328}
329
330static void
331weston_presentation_feedback_present(
332 struct weston_presentation_feedback *feedback,
333 struct weston_output *output,
334 uint32_t refresh_nsec,
335 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200336 uint64_t seq,
337 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400338{
339 struct wl_client *client = wl_resource_get_client(feedback->resource);
340 struct wl_resource *o;
341 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400342
343 wl_resource_for_each(o, &output->resource_list) {
344 if (wl_resource_get_client(o) != client)
345 continue;
346
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200347 wp_presentation_feedback_send_sync_output(feedback->resource, o);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400348 }
349
350 secs = ts->tv_sec;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200351 wp_presentation_feedback_send_presented(feedback->resource,
352 secs >> 32, secs & 0xffffffff,
353 ts->tv_nsec,
354 refresh_nsec,
355 seq >> 32, seq & 0xffffffff,
356 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400357 wl_resource_destroy(feedback->resource);
358}
359
360static void
361weston_presentation_feedback_present_list(struct wl_list *list,
362 struct weston_output *output,
363 uint32_t refresh_nsec,
364 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200365 uint64_t seq,
366 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400367{
368 struct weston_presentation_feedback *feedback, *tmp;
369
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200370 assert(!(flags & WP_PRESENTATION_FEEDBACK_INVALID) ||
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200371 wl_list_empty(list));
372
Pekka Paalanen133e4392014-09-23 22:08:46 -0400373 wl_list_for_each_safe(feedback, tmp, list, link)
374 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200375 refresh_nsec, ts, seq,
376 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400377}
378
Jason Ekstrand7b982072014-05-20 14:33:03 -0500379static void
380surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
381{
382 struct weston_surface_state *state =
383 container_of(listener, struct weston_surface_state,
384 buffer_destroy_listener);
385
386 state->buffer = NULL;
387}
388
389static void
390weston_surface_state_init(struct weston_surface_state *state)
391{
392 state->newly_attached = 0;
393 state->buffer = NULL;
394 state->buffer_destroy_listener.notify =
395 surface_state_handle_buffer_destroy;
396 state->sx = 0;
397 state->sy = 0;
398
Derek Foreman152254b2015-11-26 14:17:48 -0600399 pixman_region32_init(&state->damage_surface);
400 pixman_region32_init(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500401 pixman_region32_init(&state->opaque);
402 region_init_infinite(&state->input);
403
404 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400405 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500406
407 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
408 state->buffer_viewport.buffer.scale = 1;
409 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
410 state->buffer_viewport.surface.width = -1;
411 state->buffer_viewport.changed = 0;
412}
413
414static void
415weston_surface_state_fini(struct weston_surface_state *state)
416{
417 struct weston_frame_callback *cb, *next;
418
419 wl_list_for_each_safe(cb, next,
420 &state->frame_callback_list, link)
421 wl_resource_destroy(cb->resource);
422
Pekka Paalanen133e4392014-09-23 22:08:46 -0400423 weston_presentation_feedback_discard_list(&state->feedback_list);
424
Jason Ekstrand7b982072014-05-20 14:33:03 -0500425 pixman_region32_fini(&state->input);
426 pixman_region32_fini(&state->opaque);
Derek Foreman152254b2015-11-26 14:17:48 -0600427 pixman_region32_fini(&state->damage_surface);
428 pixman_region32_fini(&state->damage_buffer);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500429
430 if (state->buffer)
431 wl_list_remove(&state->buffer_destroy_listener.link);
432 state->buffer = NULL;
433}
434
435static void
436weston_surface_state_set_buffer(struct weston_surface_state *state,
437 struct weston_buffer *buffer)
438{
439 if (state->buffer == buffer)
440 return;
441
442 if (state->buffer)
443 wl_list_remove(&state->buffer_destroy_listener.link);
444 state->buffer = buffer;
445 if (state->buffer)
446 wl_signal_add(&state->buffer->destroy_signal,
447 &state->buffer_destroy_listener);
448}
449
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500450WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500451weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500452{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500453 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400454
Bryce Harringtonde16d892014-11-20 22:21:57 -0800455 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400456 if (surface == NULL)
457 return NULL;
458
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500459 wl_signal_init(&surface->destroy_signal);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +0800460 wl_signal_init(&surface->commit_signal);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500461
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500462 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200463 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400464
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200465 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
466 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200467 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
468 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500469
470 weston_surface_state_init(&surface->pending);
471
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400472 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500473 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300474 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400475
Jason Ekstranda7af7042013-10-12 22:38:11 -0500476 wl_list_init(&surface->views);
477
478 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400479 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500480
Pekka Paalanene67858b2013-04-25 13:57:42 +0300481 wl_list_init(&surface->subsurface_list);
482 wl_list_init(&surface->subsurface_list_pending);
483
Jason Ekstrand1e059042014-10-16 10:55:19 -0500484 weston_matrix_init(&surface->buffer_to_surface_matrix);
485 weston_matrix_init(&surface->surface_to_buffer_matrix);
486
Jonas Ådahld3414f22016-07-22 17:56:31 +0800487 wl_list_init(&surface->pointer_constraints);
488
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400489 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500490}
491
Alex Wu8811bf92012-02-28 18:07:54 +0800492WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500493weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200494 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500495{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100496 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500497}
498
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400499WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500weston_view_to_global_float(struct weston_view *view,
501 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200502{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500503 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200504 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
505
Jason Ekstranda7af7042013-10-12 22:38:11 -0500506 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200507
508 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200509 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700510 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200511 v.f[3]);
512 *x = 0;
513 *y = 0;
514 return;
515 }
516
517 *x = v.f[0] / v.f[3];
518 *y = v.f[1] / v.f[3];
519 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500520 *x = sx + view->geometry.x;
521 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200522 }
523}
524
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500525WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200526weston_transformed_coord(int width, int height,
527 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200528 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200529 float sx, float sy, float *bx, float *by)
530{
531 switch (transform) {
532 case WL_OUTPUT_TRANSFORM_NORMAL:
533 default:
534 *bx = sx;
535 *by = sy;
536 break;
537 case WL_OUTPUT_TRANSFORM_FLIPPED:
538 *bx = width - sx;
539 *by = sy;
540 break;
541 case WL_OUTPUT_TRANSFORM_90:
542 *bx = height - sy;
543 *by = sx;
544 break;
545 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
546 *bx = height - sy;
547 *by = width - sx;
548 break;
549 case WL_OUTPUT_TRANSFORM_180:
550 *bx = width - sx;
551 *by = height - sy;
552 break;
553 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
554 *bx = sx;
555 *by = height - sy;
556 break;
557 case WL_OUTPUT_TRANSFORM_270:
558 *bx = sy;
559 *by = width - sx;
560 break;
561 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
562 *bx = sy;
563 *by = sx;
564 break;
565 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200566
567 *bx *= scale;
568 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200569}
570
571WL_EXPORT pixman_box32_t
572weston_transformed_rect(int width, int height,
573 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200574 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200575 pixman_box32_t rect)
576{
577 float x1, x2, y1, y2;
578
579 pixman_box32_t ret;
580
Alexander Larsson4ea95522013-05-22 14:41:37 +0200581 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200582 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200583 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200584 rect.x2, rect.y2, &x2, &y2);
585
586 if (x1 <= x2) {
587 ret.x1 = x1;
588 ret.x2 = x2;
589 } else {
590 ret.x1 = x2;
591 ret.x2 = x1;
592 }
593
594 if (y1 <= y2) {
595 ret.y1 = y1;
596 ret.y2 = y2;
597 } else {
598 ret.y1 = y2;
599 ret.y2 = y1;
600 }
601
602 return ret;
603}
604
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600605/** Transform a region by a matrix, restricted to axis-aligned transformations
606 *
607 * Warning: This function does not work for projective, affine, or matrices
608 * that encode arbitrary rotations. Only 90-degree step rotations are
609 * supported.
610 */
611WL_EXPORT void
612weston_matrix_transform_region(pixman_region32_t *dest,
613 struct weston_matrix *matrix,
614 pixman_region32_t *src)
615{
616 pixman_box32_t *src_rects, *dest_rects;
617 int nrects, i;
618
619 src_rects = pixman_region32_rectangles(src, &nrects);
620 dest_rects = malloc(nrects * sizeof(*dest_rects));
621 if (!dest_rects)
622 return;
623
624 for (i = 0; i < nrects; i++) {
625 struct weston_vector vec1 = {{
626 src_rects[i].x1, src_rects[i].y1, 0, 1
627 }};
628 weston_matrix_transform(matrix, &vec1);
629 vec1.f[0] /= vec1.f[3];
630 vec1.f[1] /= vec1.f[3];
631
632 struct weston_vector vec2 = {{
633 src_rects[i].x2, src_rects[i].y2, 0, 1
634 }};
635 weston_matrix_transform(matrix, &vec2);
636 vec2.f[0] /= vec2.f[3];
637 vec2.f[1] /= vec2.f[3];
638
639 if (vec1.f[0] < vec2.f[0]) {
640 dest_rects[i].x1 = floor(vec1.f[0]);
641 dest_rects[i].x2 = ceil(vec2.f[0]);
642 } else {
643 dest_rects[i].x1 = floor(vec2.f[0]);
644 dest_rects[i].x2 = ceil(vec1.f[0]);
645 }
646
Derek Foremanbc9a61c2015-11-18 16:32:30 -0600647 if (vec1.f[1] < vec2.f[1]) {
648 dest_rects[i].y1 = floor(vec1.f[1]);
649 dest_rects[i].y2 = ceil(vec2.f[1]);
650 } else {
651 dest_rects[i].y1 = floor(vec2.f[1]);
652 dest_rects[i].y2 = ceil(vec1.f[1]);
653 }
654 }
655
656 pixman_region32_clear(dest);
657 pixman_region32_init_rects(dest, dest_rects, nrects);
658 free(dest_rects);
659}
660
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200661WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500662weston_transformed_region(int width, int height,
663 enum wl_output_transform transform,
664 int32_t scale,
665 pixman_region32_t *src, pixman_region32_t *dest)
666{
667 pixman_box32_t *src_rects, *dest_rects;
668 int nrects, i;
669
670 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
671 if (src != dest)
672 pixman_region32_copy(dest, src);
673 return;
674 }
675
676 src_rects = pixman_region32_rectangles(src, &nrects);
677 dest_rects = malloc(nrects * sizeof(*dest_rects));
678 if (!dest_rects)
679 return;
680
681 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
682 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
683 } else {
684 for (i = 0; i < nrects; i++) {
685 switch (transform) {
686 default:
687 case WL_OUTPUT_TRANSFORM_NORMAL:
688 dest_rects[i].x1 = src_rects[i].x1;
689 dest_rects[i].y1 = src_rects[i].y1;
690 dest_rects[i].x2 = src_rects[i].x2;
691 dest_rects[i].y2 = src_rects[i].y2;
692 break;
693 case WL_OUTPUT_TRANSFORM_90:
694 dest_rects[i].x1 = height - src_rects[i].y2;
695 dest_rects[i].y1 = src_rects[i].x1;
696 dest_rects[i].x2 = height - src_rects[i].y1;
697 dest_rects[i].y2 = src_rects[i].x2;
698 break;
699 case WL_OUTPUT_TRANSFORM_180:
700 dest_rects[i].x1 = width - src_rects[i].x2;
701 dest_rects[i].y1 = height - src_rects[i].y2;
702 dest_rects[i].x2 = width - src_rects[i].x1;
703 dest_rects[i].y2 = height - src_rects[i].y1;
704 break;
705 case WL_OUTPUT_TRANSFORM_270:
706 dest_rects[i].x1 = src_rects[i].y1;
707 dest_rects[i].y1 = width - src_rects[i].x2;
708 dest_rects[i].x2 = src_rects[i].y2;
709 dest_rects[i].y2 = width - src_rects[i].x1;
710 break;
711 case WL_OUTPUT_TRANSFORM_FLIPPED:
712 dest_rects[i].x1 = width - src_rects[i].x2;
713 dest_rects[i].y1 = src_rects[i].y1;
714 dest_rects[i].x2 = width - src_rects[i].x1;
715 dest_rects[i].y2 = src_rects[i].y2;
716 break;
717 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
718 dest_rects[i].x1 = height - src_rects[i].y2;
719 dest_rects[i].y1 = width - src_rects[i].x2;
720 dest_rects[i].x2 = height - src_rects[i].y1;
721 dest_rects[i].y2 = width - src_rects[i].x1;
722 break;
723 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
724 dest_rects[i].x1 = src_rects[i].x1;
725 dest_rects[i].y1 = height - src_rects[i].y2;
726 dest_rects[i].x2 = src_rects[i].x2;
727 dest_rects[i].y2 = height - src_rects[i].y1;
728 break;
729 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
730 dest_rects[i].x1 = src_rects[i].y1;
731 dest_rects[i].y1 = src_rects[i].x1;
732 dest_rects[i].x2 = src_rects[i].y2;
733 dest_rects[i].y2 = src_rects[i].x2;
734 break;
735 }
736 }
737 }
738
739 if (scale != 1) {
740 for (i = 0; i < nrects; i++) {
741 dest_rects[i].x1 *= scale;
742 dest_rects[i].x2 *= scale;
743 dest_rects[i].y1 *= scale;
744 dest_rects[i].y2 *= scale;
745 }
746 }
747
748 pixman_region32_clear(dest);
749 pixman_region32_init_rects(dest, dest_rects, nrects);
750 free(dest_rects);
751}
752
Jonny Lamb74130762013-11-26 18:19:46 +0100753static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300754viewport_surface_to_buffer(struct weston_surface *surface,
755 float sx, float sy, float *bx, float *by)
Jonny Lamb74130762013-11-26 18:19:46 +0100756{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200757 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200758 double src_width, src_height;
759 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200760
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200761 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
762 if (vp->surface.width == -1) {
763 *bx = sx;
764 *by = sy;
765 return;
766 }
Jonny Lamb74130762013-11-26 18:19:46 +0100767
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200768 src_x = 0.0;
769 src_y = 0.0;
770 src_width = surface->width_from_buffer;
771 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100772 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200773 src_x = wl_fixed_to_double(vp->buffer.src_x);
774 src_y = wl_fixed_to_double(vp->buffer.src_y);
775 src_width = wl_fixed_to_double(vp->buffer.src_width);
776 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100777 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200778
779 *bx = sx * src_width / surface->width + src_x;
780 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100781}
782
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500783WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200784weston_surface_to_buffer_float(struct weston_surface *surface,
785 float sx, float sy, float *bx, float *by)
786{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200787 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
788
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300789 /* first transform coordinates if the viewport is set */
790 viewport_surface_to_buffer(surface, sx, sy, bx, by);
Jonny Lamb74130762013-11-26 18:19:46 +0100791
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500792 weston_transformed_coord(surface->width_from_buffer,
793 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200794 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100795 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200796}
797
Derek Foreman99739672015-12-03 16:38:11 -0600798/** Transform a rectangle from surface coordinates to buffer coordinates
799 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300800 * \param surface The surface to fetch wp_viewport and buffer transformation
Derek Foreman99739672015-12-03 16:38:11 -0600801 * from.
802 * \param rect The rectangle to transform.
803 * \return The transformed rectangle.
804 *
805 * Viewport and buffer transformations can only do translation, scaling,
806 * and rotations in 90-degree steps. Therefore the only loss in the
807 * conversion is coordinate rounding.
808 *
809 * However, some coordinate rounding takes place as an intermediate
810 * step before the buffer scale factor is applied, so the rectangle
811 * boundary may not be exactly as expected.
812 *
813 * This is OK for damage tracking since a little extra coverage is
814 * not a problem.
815 */
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200816WL_EXPORT pixman_box32_t
817weston_surface_to_buffer_rect(struct weston_surface *surface,
818 pixman_box32_t rect)
819{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200820 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100821 float xf, yf;
822
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300823 /* first transform box coordinates if the viewport is set */
824 viewport_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100825 rect.x1 = floorf(xf);
826 rect.y1 = floorf(yf);
827
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +0300828 viewport_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
Derek Foremane2e15ac2015-12-01 13:00:43 -0600829 rect.x2 = ceilf(xf);
830 rect.y2 = ceilf(yf);
Jonny Lamb74130762013-11-26 18:19:46 +0100831
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500832 return weston_transformed_rect(surface->width_from_buffer,
833 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200834 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200835 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200836}
837
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200838/** Transform a region from surface coordinates to buffer coordinates
839 *
Pekka Paalanene95ad5c2016-04-15 14:47:08 +0300840 * \param surface The surface to fetch wp_viewport and buffer transformation
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200841 * from.
842 * \param surface_region[in] The region in surface coordinates.
843 * \param buffer_region[out] The region converted to buffer coordinates.
844 *
845 * Buffer_region must be init'd, but will be completely overwritten.
846 *
847 * Viewport and buffer transformations can only do translation, scaling,
848 * and rotations in 90-degree steps. Therefore the only loss in the
Derek Foreman99739672015-12-03 16:38:11 -0600849 * conversion is from the coordinate rounding that takes place in
850 * \ref weston_surface_to_buffer_rect.
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200851 */
852WL_EXPORT void
853weston_surface_to_buffer_region(struct weston_surface *surface,
854 pixman_region32_t *surface_region,
855 pixman_region32_t *buffer_region)
856{
857 pixman_box32_t *src_rects, *dest_rects;
858 int nrects, i;
859
860 src_rects = pixman_region32_rectangles(surface_region, &nrects);
861 dest_rects = malloc(nrects * sizeof(*dest_rects));
862 if (!dest_rects)
863 return;
864
865 for (i = 0; i < nrects; i++) {
866 dest_rects[i] = weston_surface_to_buffer_rect(surface,
867 src_rects[i]);
868 }
869
870 pixman_region32_fini(buffer_region);
871 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
872 free(dest_rects);
873}
874
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200875WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500876weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400877 struct weston_plane *plane)
878{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400880 return;
881
Jason Ekstranda7af7042013-10-12 22:38:11 -0500882 weston_view_damage_below(view);
883 view->plane = plane;
884 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400885}
886
Pekka Paalanen51723d52015-02-17 13:10:01 +0200887/** Inflict damage on the plane where the view is visible.
888 *
889 * \param view The view that causes the damage.
890 *
891 * If the view is currently on a plane (including the primary plane),
892 * take the view's boundingbox, subtract all the opaque views that cover it,
893 * and add the remaining region as damage to the plane. This corresponds
894 * to the damage inflicted to the plane if this view disappeared.
895 *
896 * A repaint is scheduled for this view.
897 *
898 * The region of all opaque views covering this view is stored in
899 * weston_view::clip and updated by view_accumulate_damage() during
900 * weston_output_repaint(). Specifically, that region matches the
901 * scenegraph as it was last painted.
902 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400903WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500904weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200905{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500906 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200907
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500908 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200909 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500910 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800911 if (view->plane)
912 pixman_region32_union(&view->plane->damage,
913 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500914 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800915 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200916}
917
Bryce Harrington3f650b82015-12-23 11:01:58 -0800918/**
919 * \param es The surface
920 * \param mask The new set of outputs for the surface
921 *
922 * Sets the surface's set of outputs to the ones specified by
923 * the new output mask provided. Identifies the outputs that
924 * have changed, the posts enter and leave events for these
925 * outputs as appropriate.
926 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400927static void
928weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
929{
930 uint32_t different = es->output_mask ^ mask;
931 uint32_t entered = mask & different;
932 uint32_t left = es->output_mask & different;
933 struct weston_output *output;
934 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500935 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400936
937 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500938 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400939 return;
940 if (different == 0)
941 return;
942
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500943 client = wl_resource_get_client(es->resource);
944
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400945 wl_list_for_each(output, &es->compositor->output_list, link) {
Bryce Harrington89324ce2015-12-23 18:38:07 -0800946 if (1u << output->id & different)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400947 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500948 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400949 client);
950 if (resource == NULL)
951 continue;
Bryce Harrington89324ce2015-12-23 18:38:07 -0800952 if (1u << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500953 wl_surface_send_enter(es->resource, resource);
Bryce Harrington89324ce2015-12-23 18:38:07 -0800954 if (1u << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500955 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400956 }
957}
958
Bryce Harrington3f650b82015-12-23 11:01:58 -0800959/** Recalculate which output(s) the surface has views displayed on
960 *
961 * \param es The surface to remap to outputs
962 *
963 * Finds the output that is showing the largest amount of one
964 * of the surface's various views. This output becomes the
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300965 * surface's primary output for vsync and frame callback purposes.
Bryce Harrington3f650b82015-12-23 11:01:58 -0800966 *
Pekka Paalanen130ae6e2016-03-30 14:33:33 +0300967 * Also notes all outputs of all of the surface's views
Bryce Harrington3f650b82015-12-23 11:01:58 -0800968 * in the output_mask for the surface.
969 */
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400970static void
971weston_surface_assign_output(struct weston_surface *es)
972{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973 struct weston_output *new_output;
974 struct weston_view *view;
975 pixman_region32_t region;
976 uint32_t max, area, mask;
977 pixman_box32_t *e;
978
979 new_output = NULL;
980 max = 0;
981 mask = 0;
982 pixman_region32_init(&region);
983 wl_list_for_each(view, &es->views, surface_link) {
984 if (!view->output)
985 continue;
986
987 pixman_region32_intersect(&region, &view->transform.boundingbox,
988 &view->output->region);
989
990 e = pixman_region32_extents(&region);
991 area = (e->x2 - e->x1) * (e->y2 - e->y1);
992
993 mask |= view->output_mask;
994
995 if (area >= max) {
996 new_output = view->output;
997 max = area;
998 }
999 }
1000 pixman_region32_fini(&region);
1001
1002 es->output = new_output;
1003 weston_surface_update_output_mask(es, mask);
1004}
1005
Bryce Harrington3f650b82015-12-23 11:01:58 -08001006/** Recalculate which output(s) the view is displayed on
1007 *
1008 * \param ev The view to remap to outputs
1009 *
1010 * Identifies the set of outputs that the view is visible on,
1011 * noting them into the output_mask. The output that the view
Pekka Paalanen130ae6e2016-03-30 14:33:33 +03001012 * is most visible on is set as the view's primary output.
Bryce Harrington3f650b82015-12-23 11:01:58 -08001013 *
1014 * Also does the same for the view's surface. See
1015 * weston_surface_assign_output().
1016 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017static void
1018weston_view_assign_output(struct weston_view *ev)
1019{
1020 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001021 struct weston_output *output, *new_output;
1022 pixman_region32_t region;
1023 uint32_t max, area, mask;
1024 pixman_box32_t *e;
1025
1026 new_output = NULL;
1027 max = 0;
1028 mask = 0;
1029 pixman_region32_init(&region);
1030 wl_list_for_each(output, &ec->output_list, link) {
Giulio Camuffo2f2a70c2015-07-12 10:52:32 +03001031 if (output->destroying)
1032 continue;
1033
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001035 &output->region);
1036
1037 e = pixman_region32_extents(&region);
1038 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1039
1040 if (area > 0)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001041 mask |= 1u << output->id;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001042
1043 if (area >= max) {
1044 new_output = output;
1045 max = area;
1046 }
1047 }
1048 pixman_region32_fini(&region);
1049
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 ev->output = new_output;
1051 ev->output_mask = mask;
1052
1053 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001054}
1055
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001056static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001057weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1058 int from_x, int from_y, int *to_x, int *to_y)
1059{
1060 float x, y;
1061
1062 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1063 weston_view_from_global_float(to, x, y, &x, &y);
1064
1065 *to_x = round(x);
1066 *to_y = round(y);
1067}
1068
1069static void
1070weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1071{
1072 pixman_box32_t *a;
1073 pixman_box32_t b;
1074
1075 a = pixman_region32_extents(&from->geometry.scissor);
1076
1077 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1078 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1079
1080 pixman_region32_fini(&to->geometry.scissor);
1081 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1082}
1083
1084static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001085view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001087{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001088 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1089 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001090 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001091 { inbox->x1, inbox->y1 },
1092 { inbox->x1, inbox->y2 },
1093 { inbox->x2, inbox->y1 },
1094 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001095 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001096 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001097 int i;
1098
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001099 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001100 /* avoid rounding empty bbox to 1x1 */
1101 pixman_region32_init(bbox);
1102 return;
1103 }
1104
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001105 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001106 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001107 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001108 if (x < min_x)
1109 min_x = x;
1110 if (x > max_x)
1111 max_x = x;
1112 if (y < min_y)
1113 min_y = y;
1114 if (y > max_y)
1115 max_y = y;
1116 }
1117
Pekka Paalanen219b9822012-02-08 15:38:37 +02001118 int_x = floorf(min_x);
1119 int_y = floorf(min_y);
1120 pixman_region32_init_rect(bbox, int_x, int_y,
1121 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001122}
1123
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001124static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001126{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001128
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001129 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 view->geometry.x = roundf(view->geometry.x);
1131 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001132
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001133 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1135 view->transform.position.matrix.d[12] = view->geometry.x;
1136 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001137
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001139
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 view->transform.inverse = view->transform.position.matrix;
1141 view->transform.inverse.d[12] = -view->geometry.x;
1142 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001143
Jason Ekstranda7af7042013-10-12 22:38:11 -05001144 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001145 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001146 view->surface->width,
1147 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001148 if (view->geometry.scissor_enabled)
1149 pixman_region32_intersect(&view->transform.boundingbox,
1150 &view->transform.boundingbox,
1151 &view->geometry.scissor);
1152
1153 pixman_region32_translate(&view->transform.boundingbox,
1154 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001155
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156 if (view->alpha == 1.0) {
1157 pixman_region32_copy(&view->transform.opaque,
1158 &view->surface->opaque);
1159 pixman_region32_translate(&view->transform.opaque,
1160 view->geometry.x,
1161 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001162 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001163}
1164
1165static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001167{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 struct weston_view *parent = view->geometry.parent;
1169 struct weston_matrix *matrix = &view->transform.matrix;
1170 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001171 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001172 pixman_region32_t surfregion;
1173 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001174
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001176
1177 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1179 view->transform.position.matrix.d[12] = view->geometry.x;
1180 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001181
1182 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001183 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001184 weston_matrix_multiply(matrix, &tform->matrix);
1185
Pekka Paalanen483243f2013-03-08 14:56:50 +02001186 if (parent)
1187 weston_matrix_multiply(matrix, &parent->transform.matrix);
1188
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001189 if (weston_matrix_invert(inverse, matrix) < 0) {
1190 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 weston_log("error: weston_view %p"
1192 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001193 return -1;
1194 }
1195
Giulio Camuffo148c1992016-09-04 18:50:46 +03001196 if (view->alpha == 1.0 &&
1197 matrix->type == WESTON_MATRIX_TRANSFORM_TRANSLATE) {
1198 pixman_region32_copy(&view->transform.opaque,
1199 &view->surface->opaque);
1200 pixman_region32_translate(&view->transform.opaque,
1201 matrix->d[12],
1202 matrix->d[13]);
1203 }
1204
Pekka Paalanen380adf52015-02-16 14:39:11 +02001205 pixman_region32_init_rect(&surfregion, 0, 0,
1206 view->surface->width, view->surface->height);
1207 if (view->geometry.scissor_enabled)
1208 pixman_region32_intersect(&surfregion, &surfregion,
1209 &view->geometry.scissor);
1210 surfbox = pixman_region32_extents(&surfregion);
1211
1212 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1213 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001214
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001215 return 0;
1216}
1217
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001218static struct weston_layer *
1219get_view_layer(struct weston_view *view)
1220{
1221 if (view->parent_view)
1222 return get_view_layer(view->parent_view);
1223 return view->layer_link.layer;
1224}
1225
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001226WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001228{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001230 struct weston_layer *layer;
1231 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001232
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001234 return;
1235
Pekka Paalanen483243f2013-03-08 14:56:50 +02001236 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001238
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001240
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001242
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 pixman_region32_fini(&view->transform.boundingbox);
1244 pixman_region32_fini(&view->transform.opaque);
1245 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001246
Pekka Paalanencd403622012-01-25 13:37:39 +02001247 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 if (view->geometry.transformation_list.next ==
1249 &view->transform.position.link &&
1250 view->geometry.transformation_list.prev ==
1251 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001252 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001254 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 if (weston_view_update_transform_enable(view) < 0)
1256 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001257 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001258
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001259 layer = get_view_layer(view);
1260 if (layer) {
1261 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001262 pixman_region32_intersect(&view->transform.boundingbox,
1263 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001264 pixman_region32_intersect(&view->transform.opaque,
1265 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001266 pixman_region32_fini(&mask);
1267 }
1268
Pekka Paalanen380adf52015-02-16 14:39:11 +02001269 if (parent) {
1270 if (parent->geometry.scissor_enabled) {
1271 view->geometry.scissor_enabled = true;
1272 weston_view_transfer_scissor(parent, view);
1273 } else {
1274 view->geometry.scissor_enabled = false;
1275 }
1276 }
1277
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001279
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001281
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 wl_signal_emit(&view->surface->compositor->transform_signal,
1283 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001284}
1285
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001286WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001288{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001290
1291 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 * The invariant: if view->geometry.dirty, then all views
1293 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001294 * Corollary: if not parent->geometry.dirty, then all ancestors
1295 * are not dirty.
1296 */
1297
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001299 return;
1300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001302
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001304 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001306}
1307
1308WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309weston_view_to_global_fixed(struct weston_view *view,
1310 wl_fixed_t vx, wl_fixed_t vy,
1311 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001312{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001313 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001314
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 weston_view_to_global_float(view,
1316 wl_fixed_to_double(vx),
1317 wl_fixed_to_double(vy),
1318 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001319 *x = wl_fixed_from_double(xf);
1320 *y = wl_fixed_from_double(yf);
1321}
1322
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001323WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324weston_view_from_global_float(struct weston_view *view,
1325 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001326{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001328 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1329
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001331
1332 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001333 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001335 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 *vx = 0;
1337 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001338 return;
1339 }
1340
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 *vx = v.f[0] / v.f[3];
1342 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001343 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 *vx = x - view->geometry.x;
1345 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001346 }
1347}
1348
1349WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350weston_view_from_global_fixed(struct weston_view *view,
1351 wl_fixed_t x, wl_fixed_t y,
1352 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001353{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001355
Jason Ekstranda7af7042013-10-12 22:38:11 -05001356 weston_view_from_global_float(view,
1357 wl_fixed_to_double(x),
1358 wl_fixed_to_double(y),
1359 &vxf, &vyf);
1360 *vx = wl_fixed_from_double(vxf);
1361 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001362}
1363
1364WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365weston_view_from_global(struct weston_view *view,
1366 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001367{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001369
Jason Ekstranda7af7042013-10-12 22:38:11 -05001370 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1371 *vx = floorf(vxf);
1372 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001373}
1374
Bryce Harrington3f650b82015-12-23 11:01:58 -08001375/**
1376 * \param surface The surface to be repainted
1377 *
1378 * Marks the output(s) that the surface is shown on as needing to be
1379 * repainted. See weston_output_schedule_repaint().
1380 */
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001381WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001382weston_surface_schedule_repaint(struct weston_surface *surface)
1383{
1384 struct weston_output *output;
1385
1386 wl_list_for_each(output, &surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001387 if (surface->output_mask & (1u << output->id))
Kristian Høgsberg98238702012-08-03 16:29:12 -04001388 weston_output_schedule_repaint(output);
1389}
1390
Bryce Harrington3f650b82015-12-23 11:01:58 -08001391/**
1392 * \param view The view to be repainted
1393 *
1394 * Marks the output(s) that the view is shown on as needing to be
1395 * repainted. See weston_output_schedule_repaint().
1396 */
Kristian Høgsberg98238702012-08-03 16:29:12 -04001397WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398weston_view_schedule_repaint(struct weston_view *view)
1399{
1400 struct weston_output *output;
1401
1402 wl_list_for_each(output, &view->surface->compositor->output_list, link)
Bryce Harrington89324ce2015-12-23 18:38:07 -08001403 if (view->output_mask & (1u << output->id))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404 weston_output_schedule_repaint(output);
1405}
1406
Pekka Paalanene508ce62015-02-19 13:59:55 +02001407/**
1408 * XXX: This function does it the wrong way.
1409 * surface->damage is the damage from the client, and causes
1410 * surface_flush_damage() to copy pixels. No window management action can
1411 * cause damage to the client-provided content, warranting re-upload!
1412 *
1413 * Instead of surface->damage, this function should record the damage
1414 * with all the views for this surface to avoid extraneous texture
1415 * uploads.
1416 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001417WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001418weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001419{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001420 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 0, 0, surface->width,
1422 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001423
Kristian Høgsberg98238702012-08-03 16:29:12 -04001424 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001425}
1426
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001427WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001429{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001430 if (view->geometry.x == x && view->geometry.y == y)
1431 return;
1432
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 view->geometry.x = x;
1434 view->geometry.y = y;
1435 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001436}
1437
Pekka Paalanen483243f2013-03-08 14:56:50 +02001438static void
1439transform_parent_handle_parent_destroy(struct wl_listener *listener,
1440 void *data)
1441{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442 struct weston_view *view =
1443 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001444 geometry.parent_destroy_listener);
1445
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001447}
1448
1449WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001451 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001452{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 if (view->geometry.parent) {
1454 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1455 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001456
1457 if (!parent)
1458 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001459 }
1460
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001462
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001464 transform_parent_handle_parent_destroy;
1465 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001466 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001467 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001468 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001470 }
1471
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 weston_view_geometry_dirty(view);
1473}
1474
Pekka Paalanen380adf52015-02-16 14:39:11 +02001475/** Set a clip mask rectangle on a view
1476 *
1477 * \param view The view to set the clip mask on.
1478 * \param x Top-left corner X coordinate of the clip rectangle.
1479 * \param y Top-left corner Y coordinate of the clip rectangle.
1480 * \param width Width of the clip rectangle, non-negative.
1481 * \param height Height of the clip rectangle, non-negative.
1482 *
1483 * A shell may set a clip mask rectangle on a view. Everything outside
1484 * the rectangle is cut away for input and output purposes: it is
1485 * not drawn and cannot be hit by hit-test based input like pointer
1486 * motion or touch-downs. Everything inside the rectangle will behave
1487 * normally. Clients are unaware of clipping.
1488 *
Yong Bakos4c72e292016-04-28 11:59:10 -05001489 * The rectangle is set in surface-local coordinates. Setting a clip
Pekka Paalanen380adf52015-02-16 14:39:11 +02001490 * mask rectangle does not affect the view position, the view is positioned
1491 * as it would be without a clip. The clip also does not change
1492 * weston_surface::width,height.
1493 *
1494 * The clip mask rectangle is part of transformation inheritance
1495 * (weston_view_set_transform_parent()). A clip set in the root of the
1496 * transformation inheritance tree will affect all views in the tree.
1497 * A clip can be set only on the root view. Attempting to set a clip
1498 * on view that has a transformation parent will fail. Assigning a parent
1499 * to a view that has a clip set will cause the clip to be forgotten.
1500 *
1501 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1502 * on the additional transformations in the child views. These transformations
1503 * may not rotate the coordinate axes, i.e., only translation and scaling
1504 * are allowed. Violating this restriction causes the clipping to malfunction.
1505 * Furthermore, using scaling may cause rounding errors in child clipping.
1506 *
1507 * The clip mask rectangle is not automatically adjusted based on
1508 * wl_surface.attach dx and dy arguments.
1509 *
1510 * A clip mask rectangle can be set only if the compositor capability
1511 * WESTON_CAP_VIEW_CLIP_MASK is present.
1512 *
1513 * This function sets the clip mask rectangle and schedules a repaint for
1514 * the view.
1515 */
1516WL_EXPORT void
1517weston_view_set_mask(struct weston_view *view,
1518 int x, int y, int width, int height)
1519{
1520 struct weston_compositor *compositor = view->surface->compositor;
1521
1522 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1523 weston_log("%s not allowed without capability!\n", __func__);
1524 return;
1525 }
1526
1527 if (view->geometry.parent) {
1528 weston_log("view %p has a parent, clip forbidden!\n", view);
1529 return;
1530 }
1531
1532 if (width < 0 || height < 0) {
1533 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1534 x, y, width, height);
1535 return;
1536 }
1537
1538 pixman_region32_fini(&view->geometry.scissor);
1539 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1540 view->geometry.scissor_enabled = true;
1541 weston_view_geometry_dirty(view);
1542 weston_view_schedule_repaint(view);
1543}
1544
1545/** Remove the clip mask from a view
1546 *
1547 * \param view The view to remove the clip mask from.
1548 *
1549 * Removed the clip mask rectangle and schedules a repaint.
1550 *
1551 * \sa weston_view_set_mask
1552 */
1553WL_EXPORT void
1554weston_view_set_mask_infinite(struct weston_view *view)
1555{
1556 view->geometry.scissor_enabled = false;
1557 weston_view_geometry_dirty(view);
1558 weston_view_schedule_repaint(view);
1559}
1560
Armin Krezović0da12b82016-06-30 06:04:33 +02001561/* Check if view should be displayed
1562 *
1563 * The indicator is set manually when assigning
1564 * a view to a surface.
1565 *
1566 * This needs reworking. See the thread starting at:
1567 *
1568 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1569 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001570WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571weston_view_is_mapped(struct weston_view *view)
1572{
Armin Krezović0da12b82016-06-30 06:04:33 +02001573 return view->is_mapped;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001574}
1575
Armin Krezović0da12b82016-06-30 06:04:33 +02001576/* Check if a surface has a view assigned to it
1577 *
1578 * The indicator is set manually when mapping
1579 * a surface and creating a view for it.
1580 *
1581 * This needs to go. See the thread starting at:
1582 *
1583 * https://lists.freedesktop.org/archives/wayland-devel/2016-June/029656.html
1584 *
1585 */
Derek Foreman280e7dd2014-10-03 13:13:42 -05001586WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001587weston_surface_is_mapped(struct weston_surface *surface)
1588{
Armin Krezović0da12b82016-06-30 06:04:33 +02001589 return surface->is_mapped;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001590}
1591
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001592static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001593surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001594{
1595 struct weston_view *view;
1596
1597 if (surface->width == width && surface->height == height)
1598 return;
1599
1600 surface->width = width;
1601 surface->height = height;
1602
1603 wl_list_for_each(view, &surface->views, surface_link)
1604 weston_view_geometry_dirty(view);
1605}
1606
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001607WL_EXPORT void
1608weston_surface_set_size(struct weston_surface *surface,
1609 int32_t width, int32_t height)
1610{
1611 assert(!surface->resource);
1612 surface_set_size(surface, width, height);
1613}
1614
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001615static int
1616fixed_round_up_to_int(wl_fixed_t f)
1617{
1618 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1619}
1620
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001621static void
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001622convert_size_by_transform_scale(int32_t *width_out, int32_t *height_out,
1623 int32_t width, int32_t height,
1624 uint32_t transform,
1625 int32_t scale)
1626{
1627 assert(scale > 0);
1628
1629 switch (transform) {
1630 case WL_OUTPUT_TRANSFORM_NORMAL:
1631 case WL_OUTPUT_TRANSFORM_180:
1632 case WL_OUTPUT_TRANSFORM_FLIPPED:
1633 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1634 *width_out = width / scale;
1635 *height_out = height / scale;
1636 break;
1637 case WL_OUTPUT_TRANSFORM_90:
1638 case WL_OUTPUT_TRANSFORM_270:
1639 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1640 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1641 *width_out = height / scale;
1642 *height_out = width / scale;
1643 break;
1644 default:
1645 assert(0 && "invalid transform");
1646 }
1647}
1648
1649static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001650weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001651{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001652 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001653
1654 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001655 surface->width_from_buffer = 0;
1656 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001657 return;
1658 }
1659
Pekka Paalanen59987fa2016-04-26 15:50:59 +03001660 convert_size_by_transform_scale(&surface->width_from_buffer,
1661 &surface->height_from_buffer,
1662 surface->buffer_ref.buffer->width,
1663 surface->buffer_ref.buffer->height,
1664 vp->buffer.transform,
1665 vp->buffer.scale);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001666}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001667
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001668static void
1669weston_surface_update_size(struct weston_surface *surface)
1670{
1671 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1672 int32_t width, height;
1673
1674 width = surface->width_from_buffer;
1675 height = surface->height_from_buffer;
1676
1677 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001678 surface_set_size(surface,
1679 vp->surface.width, vp->surface.height);
1680 return;
1681 }
1682
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001683 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001684 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1685 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1686
1687 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001688 return;
1689 }
1690
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001691 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001692}
1693
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001694WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001695weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001696{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001697 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001698
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001699 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001700
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001701 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001702}
1703
Jason Ekstranda7af7042013-10-12 22:38:11 -05001704WL_EXPORT struct weston_view *
1705weston_compositor_pick_view(struct weston_compositor *compositor,
1706 wl_fixed_t x, wl_fixed_t y,
1707 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001708{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001710 wl_fixed_t view_x, view_y;
1711 int view_ix, view_iy;
1712 int ix = wl_fixed_to_int(x);
1713 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001714
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001716 if (!pixman_region32_contains_point(
1717 &view->transform.boundingbox, ix, iy, NULL))
1718 continue;
1719
1720 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1721 view_ix = wl_fixed_to_int(view_x);
1722 view_iy = wl_fixed_to_int(view_y);
1723
1724 if (!pixman_region32_contains_point(&view->surface->input,
1725 view_ix, view_iy, NULL))
1726 continue;
1727
Pekka Paalanen380adf52015-02-16 14:39:11 +02001728 if (view->geometry.scissor_enabled &&
1729 !pixman_region32_contains_point(&view->geometry.scissor,
1730 view_ix, view_iy, NULL))
1731 continue;
1732
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001733 *vx = view_x;
1734 *vy = view_y;
1735 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001736 }
1737
Derek Foremanf9318d12015-05-11 15:40:11 -05001738 *vx = wl_fixed_from_int(-1000000);
1739 *vy = wl_fixed_from_int(-1000000);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001740 return NULL;
1741}
1742
1743static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001744weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001745{
Daniel Stone37816df2012-05-16 18:45:18 +01001746 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001747
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001748 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001749 return;
1750
Daniel Stone37816df2012-05-16 18:45:18 +01001751 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001752 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001753}
1754
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001755WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001756weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001757{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001758 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001759
Jason Ekstranda7af7042013-10-12 22:38:11 -05001760 if (!weston_view_is_mapped(view))
1761 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001762
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763 weston_view_damage_below(view);
1764 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001765 view->plane = NULL;
Armin Krezovićf8486c32016-06-30 06:04:28 +02001766 view->is_mapped = false;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001767 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001768 wl_list_remove(&view->link);
1769 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770 view->output_mask = 0;
1771 weston_surface_assign_output(view->surface);
1772
1773 if (weston_surface_is_mapped(view->surface))
1774 return;
1775
1776 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001777 struct weston_touch *touch = weston_seat_get_touch(seat);
1778 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1779 struct weston_keyboard *keyboard =
1780 weston_seat_get_keyboard(seat);
1781
1782 if (keyboard && keyboard->focus == view->surface)
1783 weston_keyboard_set_focus(keyboard, NULL);
1784 if (pointer && pointer->focus == view)
Derek Foremanf9318d12015-05-11 15:40:11 -05001785 weston_pointer_clear_focus(pointer);
Derek Foreman1281a362015-07-31 16:55:32 -05001786 if (touch && touch->focus == view)
1787 weston_touch_set_focus(touch, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001788 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001790
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791WL_EXPORT void
1792weston_surface_unmap(struct weston_surface *surface)
1793{
1794 struct weston_view *view;
1795
Armin Krezovićf8486c32016-06-30 06:04:28 +02001796 surface->is_mapped = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797 wl_list_for_each(view, &surface->views, surface_link)
1798 weston_view_unmap(view);
1799 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001800}
1801
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001802static void
1803weston_surface_reset_pending_buffer(struct weston_surface *surface)
1804{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001805 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001806 surface->pending.sx = 0;
1807 surface->pending.sy = 0;
1808 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001809 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001810}
1811
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812WL_EXPORT void
1813weston_view_destroy(struct weston_view *view)
1814{
1815 wl_signal_emit(&view->destroy_signal, view);
1816
1817 assert(wl_list_empty(&view->geometry.child_list));
1818
1819 if (weston_view_is_mapped(view)) {
1820 weston_view_unmap(view);
1821 weston_compositor_build_view_list(view->surface->compositor);
1822 }
1823
1824 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001825 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826
1827 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001828 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001830 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001831
1832 weston_view_set_transform_parent(view, NULL);
1833
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 wl_list_remove(&view->surface_link);
1835
1836 free(view);
1837}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001838
1839WL_EXPORT void
1840weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001841{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001842 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001843 struct weston_view *ev, *nv;
Jonas Ådahld3414f22016-07-22 17:56:31 +08001844 struct weston_pointer_constraint *constraint, *next_constraint;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001845
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001846 if (--surface->ref_count > 0)
1847 return;
1848
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001849 assert(surface->resource == NULL);
1850
Pekka Paalanenca790762015-04-17 14:23:38 +03001851 wl_signal_emit(&surface->destroy_signal, surface);
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001852
Pekka Paalanene67858b2013-04-25 13:57:42 +03001853 assert(wl_list_empty(&surface->subsurface_list_pending));
1854 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001855
Jason Ekstranda7af7042013-10-12 22:38:11 -05001856 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1857 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001858
Jason Ekstrand7b982072014-05-20 14:33:03 -05001859 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001860
Pekka Paalanende685b82012-12-04 15:58:12 +02001861 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001862
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001863 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001864 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001865 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001866
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001867 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001868 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001869
Pekka Paalanen133e4392014-09-23 22:08:46 -04001870 weston_presentation_feedback_discard_list(&surface->feedback_list);
1871
Jonas Ådahld3414f22016-07-22 17:56:31 +08001872 wl_list_for_each_safe(constraint, next_constraint,
1873 &surface->pointer_constraints,
1874 link)
1875 weston_pointer_constraint_destroy(constraint);
1876
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001877 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001878}
1879
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001880static void
1881destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001882{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001883 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001884
Pekka Paalanen08d3fb72015-04-17 14:00:24 +03001885 assert(surface);
1886
Giulio Camuffo0d379742013-11-15 22:06:15 +01001887 /* Set the resource to NULL, since we don't want to leave a
1888 * dangling pointer if the surface was refcounted and survives
1889 * the weston_surface_destroy() call. */
1890 surface->resource = NULL;
Pekka Paalanen4826f872016-04-22 14:14:38 +03001891
1892 if (surface->viewport_resource)
1893 wl_resource_set_user_data(surface->viewport_resource, NULL);
1894
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001895 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001896}
1897
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001898static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001899weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1900{
1901 struct weston_buffer *buffer =
1902 container_of(listener, struct weston_buffer, destroy_listener);
1903
1904 wl_signal_emit(&buffer->destroy_signal, buffer);
1905 free(buffer);
1906}
1907
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001908WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001909weston_buffer_from_resource(struct wl_resource *resource)
1910{
1911 struct weston_buffer *buffer;
1912 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001913
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001914 listener = wl_resource_get_destroy_listener(resource,
1915 weston_buffer_destroy_handler);
1916
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001917 if (listener)
1918 return container_of(listener, struct weston_buffer,
1919 destroy_listener);
1920
1921 buffer = zalloc(sizeof *buffer);
1922 if (buffer == NULL)
1923 return NULL;
1924
1925 buffer->resource = resource;
1926 wl_signal_init(&buffer->destroy_signal);
1927 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001928 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001929 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001930
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001931 return buffer;
1932}
1933
1934static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001935weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1936 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001937{
Pekka Paalanende685b82012-12-04 15:58:12 +02001938 struct weston_buffer_reference *ref =
1939 container_of(listener, struct weston_buffer_reference,
1940 destroy_listener);
1941
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001942 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001943 ref->buffer = NULL;
1944}
1945
1946WL_EXPORT void
1947weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001948 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001949{
1950 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001951 ref->buffer->busy_count--;
1952 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001953 assert(wl_resource_get_client(ref->buffer->resource));
1954 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001955 WL_BUFFER_RELEASE);
1956 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001957 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001958 }
1959
Pekka Paalanende685b82012-12-04 15:58:12 +02001960 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001961 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001962 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001963 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001964 }
1965
Pekka Paalanende685b82012-12-04 15:58:12 +02001966 ref->buffer = buffer;
1967 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1968}
1969
1970static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001971weston_surface_attach(struct weston_surface *surface,
1972 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001973{
1974 weston_buffer_reference(&surface->buffer_ref, buffer);
1975
Pekka Paalanena6421c42012-12-04 15:58:10 +02001976 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001977 if (weston_surface_is_mapped(surface))
1978 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001979 }
1980
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001981 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001982
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001983 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001984 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001985}
1986
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001987WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001988weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001989{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001990 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001991
1992 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001993 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001994}
1995
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001996WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001997weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001998{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001999 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002000
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002001 pixman_region32_union(&compositor->primary_plane.damage,
2002 &compositor->primary_plane.damage,
2003 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002004 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002005}
2006
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002007static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002008surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002009{
Pekka Paalanende685b82012-12-04 15:58:12 +02002010 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002011 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002012 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002013
Pekka Paalanenb5026542014-11-12 15:09:24 +02002014 if (weston_timeline_enabled_ &&
2015 pixman_region32_not_empty(&surface->damage))
2016 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2017 TLP_OUTPUT(surface->output), TLP_END);
2018
Jason Ekstrandef540082014-06-26 10:37:36 -07002019 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020}
2021
2022static void
2023view_accumulate_damage(struct weston_view *view,
2024 pixman_region32_t *opaque)
2025{
2026 pixman_region32_t damage;
2027
2028 pixman_region32_init(&damage);
2029 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002030 pixman_box32_t *extents;
2031
Jason Ekstranda7af7042013-10-12 22:38:11 -05002032 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002033 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002034 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 pixman_region32_copy(&damage, &view->surface->damage);
2036 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002037 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002038 }
2039
Pekka Paalanen380adf52015-02-16 14:39:11 +02002040 pixman_region32_intersect(&damage, &damage,
2041 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002042 pixman_region32_subtract(&damage, &damage, opaque);
2043 pixman_region32_union(&view->plane->damage,
2044 &view->plane->damage, &damage);
2045 pixman_region32_fini(&damage);
2046 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002047 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002048}
2049
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002050static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002051compositor_accumulate_damage(struct weston_compositor *ec)
2052{
2053 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002054 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002055 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002056
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002057 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002058
2059 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002060 pixman_region32_copy(&plane->clip, &clip);
2061
2062 pixman_region32_init(&opaque);
2063
Jason Ekstranda7af7042013-10-12 22:38:11 -05002064 wl_list_for_each(ev, &ec->view_list, link) {
2065 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002066 continue;
2067
Jason Ekstranda7af7042013-10-12 22:38:11 -05002068 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002069 }
2070
2071 pixman_region32_union(&clip, &clip, &opaque);
2072 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002073 }
2074
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002075 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002076
Jason Ekstranda7af7042013-10-12 22:38:11 -05002077 wl_list_for_each(ev, &ec->view_list, link)
Derek Foreman060cf112015-11-18 16:32:26 -06002078 ev->surface->touched = false;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079
2080 wl_list_for_each(ev, &ec->view_list, link) {
2081 if (ev->surface->touched)
2082 continue;
Derek Foreman060cf112015-11-18 16:32:26 -06002083 ev->surface->touched = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002084
2085 surface_flush_damage(ev->surface);
2086
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002087 /* Both the renderer and the backend have seen the buffer
2088 * by now. If renderer needs the buffer, it has its own
2089 * reference set. If the backend wants to keep the buffer
2090 * around for migrating the surface into a non-primary plane
2091 * later, keep_buffer is true. Otherwise, drop the core
2092 * reference now, and allow early buffer release. This enables
2093 * clients to use single-buffering.
2094 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002095 if (!ev->surface->keep_buffer)
2096 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002097 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002098}
2099
2100static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002101surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002102{
2103 struct weston_subsurface *sub;
2104
Pekka Paalanene67858b2013-04-25 13:57:42 +03002105 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002107 continue;
2108
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2110 wl_list_init(&sub->surface->views);
2111
2112 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002113 }
2114}
2115
2116static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002117surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002118{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002119 struct weston_subsurface *sub;
2120 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002121
Jason Ekstranda7af7042013-10-12 22:38:11 -05002122 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2123 if (sub->surface == surface)
2124 continue;
2125
George Kiagiadakised04d382014-06-13 18:10:26 +02002126 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2127 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002128 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002129 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002130
2131 surface_free_unused_subsurface_views(sub->surface);
2132 }
2133}
2134
2135static void
2136view_list_add_subsurface_view(struct weston_compositor *compositor,
2137 struct weston_subsurface *sub,
2138 struct weston_view *parent)
2139{
2140 struct weston_subsurface *child;
2141 struct weston_view *view = NULL, *iv;
2142
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002143 if (!weston_surface_is_mapped(sub->surface))
2144 return;
2145
Jason Ekstranda7af7042013-10-12 22:38:11 -05002146 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2147 if (iv->geometry.parent == parent) {
2148 view = iv;
2149 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002150 }
2151 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002152
2153 if (view) {
2154 /* Put it back in the surface's list of views */
2155 wl_list_remove(&view->surface_link);
2156 wl_list_insert(&sub->surface->views, &view->surface_link);
2157 } else {
2158 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002159 weston_view_set_position(view,
2160 sub->position.x,
2161 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002162 weston_view_set_transform_parent(view, parent);
2163 }
2164
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002165 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002166 weston_view_update_transform(view);
Armin Krezovićf8486c32016-06-30 06:04:28 +02002167 view->is_mapped = true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002168
Pekka Paalanenb188e912013-11-19 14:03:35 +02002169 if (wl_list_empty(&sub->surface->subsurface_list)) {
2170 wl_list_insert(compositor->view_list.prev, &view->link);
2171 return;
2172 }
2173
2174 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2175 if (child->surface == sub->surface)
2176 wl_list_insert(compositor->view_list.prev, &view->link);
2177 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002179 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180}
2181
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002182/* This recursively adds the sub-surfaces for a view, relying on the
2183 * sub-surface order. Thus, if a client restacks the sub-surfaces, that
2184 * change first happens to the sub-surface list, and then automatically
2185 * propagates here. See weston_surface_damage_subsurfaces() for how the
2186 * sub-surfaces receive damage when the client changes the state.
2187 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002188static void
2189view_list_add(struct weston_compositor *compositor,
2190 struct weston_view *view)
2191{
2192 struct weston_subsurface *sub;
2193
2194 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002195
Pekka Paalanenb188e912013-11-19 14:03:35 +02002196 if (wl_list_empty(&view->surface->subsurface_list)) {
2197 wl_list_insert(compositor->view_list.prev, &view->link);
2198 return;
2199 }
2200
2201 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2202 if (sub->surface == view->surface)
2203 wl_list_insert(compositor->view_list.prev, &view->link);
2204 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002205 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002206 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002207}
2208
2209static void
2210weston_compositor_build_view_list(struct weston_compositor *compositor)
2211{
2212 struct weston_view *view;
2213 struct weston_layer *layer;
2214
2215 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002216 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002217 surface_stash_subsurface_views(view->surface);
2218
2219 wl_list_init(&compositor->view_list);
2220 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002221 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002222 view_list_add(compositor, view);
2223 }
2224 }
2225
2226 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002227 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002228 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002229}
2230
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002231static void
2232weston_output_take_feedback_list(struct weston_output *output,
2233 struct weston_surface *surface)
2234{
2235 struct weston_view *view;
2236 struct weston_presentation_feedback *feedback;
2237 uint32_t flags = 0xffffffff;
2238
2239 if (wl_list_empty(&surface->feedback_list))
2240 return;
2241
2242 /* All views must have the flag for the flag to survive. */
2243 wl_list_for_each(view, &surface->views, surface_link) {
2244 /* ignore views that are not on this output at all */
2245 if (view->output_mask & (1u << output->id))
2246 flags &= view->psf_flags;
2247 }
2248
2249 wl_list_for_each(feedback, &surface->feedback_list, link)
2250 feedback->psf_flags = flags;
2251
2252 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2253 wl_list_init(&surface->feedback_list);
2254}
2255
David Herrmann1edf44c2013-10-22 17:11:26 +02002256static int
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002257weston_output_repaint(struct weston_output *output, void *repaint_data)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002258{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002259 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002260 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002261 struct weston_animation *animation, *next;
2262 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002263 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002264 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002265 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002266
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002267 if (output->destroying)
2268 return 0;
2269
Pekka Paalanenb5026542014-11-12 15:09:24 +02002270 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2271
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002272 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002273 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002274
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002275 if (output->assign_planes && !output->disable_planes) {
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002276 output->assign_planes(output, repaint_data);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002277 } else {
2278 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002279 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002280 ev->psf_flags = 0;
2281 }
2282 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002283
Pekka Paalanene67858b2013-04-25 13:57:42 +03002284 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002285 wl_list_for_each(ev, &ec->view_list, link) {
2286 /* Note: This operation is safe to do multiple times on the
2287 * same surface.
2288 */
2289 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002290 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002291 &ev->surface->frame_callback_list);
2292 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002293
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002294 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002295 }
2296 }
2297
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002298 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002299
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002300 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002301 pixman_region32_intersect(&output_damage,
2302 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002303 pixman_region32_subtract(&output_damage,
2304 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002305
Scott Moreauccbf29d2012-02-22 14:21:41 -07002306 if (output->dirty)
2307 weston_output_update_matrix(output);
2308
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002309 r = output->repaint(output, &output_damage, repaint_data);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002310
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002311 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002312
Daniel Stone09a97e22017-03-01 11:34:06 +00002313 output->repaint_needed = false;
Daniel Stone05df8c12017-03-03 16:59:42 +00002314 if (r == 0)
2315 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002316
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002317 weston_compositor_repick(ec);
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002318
Jonas Ådahldb773762012-06-13 00:01:21 +02002319 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002320 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002321 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002322 }
2323
Scott Moreaud64cf212012-06-08 19:40:54 -06002324 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002325 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002326 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002327 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002328
Pekka Paalanenb5026542014-11-12 15:09:24 +02002329 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2330
David Herrmann1edf44c2013-10-22 17:11:26 +02002331 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002332}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002333
Pekka Paalanen82919792014-05-21 13:51:49 +03002334static void
2335weston_output_schedule_repaint_reset(struct weston_output *output)
2336{
Daniel Stone05df8c12017-03-03 16:59:42 +00002337 output->repaint_status = REPAINT_NOT_SCHEDULED;
Pekka Paalanen82919792014-05-21 13:51:49 +03002338 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
Pekka Paalanen82919792014-05-21 13:51:49 +03002339}
2340
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002341static int
2342weston_output_maybe_repaint(struct weston_output *output, struct timespec *now,
2343 void *repaint_data)
Pekka Paalanen0513a952014-05-21 16:17:27 +03002344{
Pekka Paalanen0513a952014-05-21 16:17:27 +03002345 struct weston_compositor *compositor = output->compositor;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002346 int ret = 0;
Daniel Stone6847b852017-03-01 11:34:08 +00002347 int64_t msec_to_repaint;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002348
Daniel Stone6847b852017-03-01 11:34:08 +00002349 /* We're not ready yet; come back to make a decision later. */
2350 if (output->repaint_status != REPAINT_SCHEDULED)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002351 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002352
2353 msec_to_repaint = timespec_sub_to_msec(&output->next_repaint, now);
2354 if (msec_to_repaint > 1)
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002355 return ret;
Daniel Stone05df8c12017-03-03 16:59:42 +00002356
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002357 /* If we're sleeping, drop the repaint machinery entirely; we will
2358 * explicitly repaint all outputs when we come back. */
2359 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2360 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
2361 goto err;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002362
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002363 /* We don't actually need to repaint this output; drop it from
2364 * repaint until something causes damage. */
2365 if (!output->repaint_needed)
2366 goto err;
2367
2368 /* If repaint fails, we aren't going to get weston_output_finish_frame
2369 * to trigger a new repaint, so drop it from repaint and hope
Daniel Stone6847b852017-03-01 11:34:08 +00002370 * something schedules a successful repaint later. As repainting may
2371 * take some time, re-read our clock as a courtesy to the next
2372 * output. */
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002373 ret = weston_output_repaint(output, repaint_data);
Daniel Stone6847b852017-03-01 11:34:08 +00002374 weston_compositor_read_presentation_clock(compositor, now);
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002375 if (ret != 0)
2376 goto err;
2377
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002378 return ret;
Daniel Stonecd1a1c32017-01-16 15:38:54 +00002379
2380err:
Pekka Paalanen0513a952014-05-21 16:17:27 +03002381 weston_output_schedule_repaint_reset(output);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002382 return ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002383}
2384
2385static void
2386output_repaint_timer_arm(struct weston_compositor *compositor)
2387{
2388 struct weston_output *output;
2389 bool any_should_repaint = false;
2390 struct timespec now;
Sergi Granellb4c08862017-03-18 13:01:15 +01002391 int64_t msec_to_next = INT64_MAX;
Daniel Stone6847b852017-03-01 11:34:08 +00002392
2393 weston_compositor_read_presentation_clock(compositor, &now);
2394
2395 wl_list_for_each(output, &compositor->output_list, link) {
2396 int64_t msec_to_this;
2397
2398 if (output->repaint_status != REPAINT_SCHEDULED)
2399 continue;
2400
2401 msec_to_this = timespec_sub_to_msec(&output->next_repaint,
2402 &now);
2403 if (!any_should_repaint || msec_to_this < msec_to_next)
2404 msec_to_next = msec_to_this;
2405
2406 any_should_repaint = true;
2407 }
2408
2409 if (!any_should_repaint)
2410 return;
2411
2412 /* Even if we should repaint immediately, add the minimum 1 ms delay.
2413 * This is a workaround to allow coalescing multiple output repaints
2414 * particularly from weston_output_finish_frame()
2415 * into the same call, which would not happen if we called
2416 * output_repaint_timer_handler() directly.
2417 */
2418 if (msec_to_next < 1)
2419 msec_to_next = 1;
2420
2421 wl_event_source_timer_update(compositor->repaint_timer, msec_to_next);
2422}
2423
2424static int
2425output_repaint_timer_handler(void *data)
2426{
2427 struct weston_compositor *compositor = data;
2428 struct weston_output *output;
2429 struct timespec now;
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002430 void *repaint_data = NULL;
2431 int ret;
Daniel Stone6847b852017-03-01 11:34:08 +00002432
2433 weston_compositor_read_presentation_clock(compositor, &now);
Daniel Stoneb1f166d2017-03-01 11:34:10 +00002434
2435 if (compositor->backend->repaint_begin)
2436 repaint_data = compositor->backend->repaint_begin(compositor);
2437
2438 wl_list_for_each(output, &compositor->output_list, link) {
2439 ret = weston_output_maybe_repaint(output, &now, repaint_data);
2440 if (ret)
2441 break;
2442 }
2443
2444 if (ret == 0) {
2445 if (compositor->backend->repaint_flush)
2446 compositor->backend->repaint_flush(compositor,
2447 repaint_data);
2448 } else {
2449 if (compositor->backend->repaint_cancel)
2450 compositor->backend->repaint_cancel(compositor,
2451 repaint_data);
2452 }
Daniel Stone6847b852017-03-01 11:34:08 +00002453
2454 output_repaint_timer_arm(compositor);
2455
Pekka Paalanen0513a952014-05-21 16:17:27 +03002456 return 0;
2457}
2458
Kristian Høgsbergef044142011-06-21 15:02:12 -04002459WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002460weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002461 const struct timespec *stamp,
2462 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002463{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002464 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002465 int32_t refresh_nsec;
2466 struct timespec now;
Daniel Stone6847b852017-03-01 11:34:08 +00002467 int64_t msec_rel;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002468
Pekka Paalanenb5026542014-11-12 15:09:24 +02002469 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2470 TLP_VBLANK(stamp), TLP_END);
2471
Daniel Stone05df8c12017-03-03 16:59:42 +00002472 assert(output->repaint_status == REPAINT_AWAITING_COMPLETION);
Daniel Stone3615ce12017-03-01 11:34:05 +00002473 assert(stamp || (presented_flags & WP_PRESENTATION_FEEDBACK_INVALID));
2474
Daniel Stone6847b852017-03-01 11:34:08 +00002475 weston_compositor_read_presentation_clock(compositor, &now);
2476
Daniel Stone3615ce12017-03-01 11:34:05 +00002477 /* If we haven't been supplied any timestamp at all, we don't have a
2478 * timebase to work against, so any delay just wastes time. Push a
2479 * repaint as soon as possible so we can get on with it. */
Daniel Stone6847b852017-03-01 11:34:08 +00002480 if (!stamp) {
2481 output->next_repaint = now;
Daniel Stone3615ce12017-03-01 11:34:05 +00002482 goto out;
Daniel Stone6847b852017-03-01 11:34:08 +00002483 }
Daniel Stone3615ce12017-03-01 11:34:05 +00002484
Pekka Paalanend7894d02015-07-03 15:08:53 +03002485 refresh_nsec = millihz_to_nsec(output->current_mode->refresh);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002486 weston_presentation_feedback_present_list(&output->feedback_list,
2487 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002488 output->msc,
2489 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002490
Daniel Stone37ad7e32017-03-01 11:34:02 +00002491 output->frame_time = timespec_to_msec(stamp);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002492
Daniel Stone6847b852017-03-01 11:34:08 +00002493 timespec_add_nsec(&output->next_repaint, stamp, refresh_nsec);
2494 timespec_add_msec(&output->next_repaint, &output->next_repaint,
2495 -compositor->repaint_msec);
2496 msec_rel = timespec_sub_to_msec(&output->next_repaint, &now);
Daniel Stone84aff5c2017-03-01 11:34:04 +00002497
2498 if (msec_rel < -1000 || msec_rel > 1000) {
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002499 static bool warned;
2500
2501 if (!warned)
2502 weston_log("Warning: computed repaint delay is "
Daniel Stone6847b852017-03-01 11:34:08 +00002503 "insane: %lld msec\n", (long long) msec_rel);
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002504 warned = true;
2505
Daniel Stone6847b852017-03-01 11:34:08 +00002506 output->next_repaint = now;
Pekka Paalanen8fd4de42015-03-19 12:27:29 +02002507 }
2508
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002509 /* Called from restart_repaint_loop and restart happens already after
2510 * the deadline given by repaint_msec? In that case we delay until
2511 * the deadline of the next frame, to give clients a more predictable
2512 * timing of the repaint cycle to lock on. */
Daniel Stoneeca5cca2017-02-28 21:53:51 +00002513 if (presented_flags == WP_PRESENTATION_FEEDBACK_INVALID &&
2514 msec_rel < 0) {
2515 while (timespec_sub_to_nsec(&output->next_repaint, &now) < 0) {
2516 timespec_add_nsec(&output->next_repaint,
2517 &output->next_repaint,
2518 refresh_nsec);
2519 }
2520 }
Mario Kleinerb7df04e2015-06-21 21:25:15 +02002521
Daniel Stone3615ce12017-03-01 11:34:05 +00002522out:
Daniel Stone05df8c12017-03-03 16:59:42 +00002523 output->repaint_status = REPAINT_SCHEDULED;
Daniel Stone6847b852017-03-01 11:34:08 +00002524 output_repaint_timer_arm(compositor);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002525}
2526
2527static void
2528idle_repaint(void *data)
2529{
2530 struct weston_output *output = data;
2531
Daniel Stone05df8c12017-03-03 16:59:42 +00002532 assert(output->repaint_status == REPAINT_BEGIN_FROM_IDLE);
2533 output->repaint_status = REPAINT_AWAITING_COMPLETION;
Jonas Ådahle5a12252013-04-05 23:07:11 +02002534 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002535}
2536
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002537WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002538weston_layer_entry_insert(struct weston_layer_entry *list,
2539 struct weston_layer_entry *entry)
2540{
2541 wl_list_insert(&list->link, &entry->link);
2542 entry->layer = list->layer;
2543}
2544
2545WL_EXPORT void
2546weston_layer_entry_remove(struct weston_layer_entry *entry)
2547{
2548 wl_list_remove(&entry->link);
2549 wl_list_init(&entry->link);
2550 entry->layer = NULL;
2551}
2552
Quentin Glidic82681572016-12-17 13:40:51 +01002553
2554/** Initialize the weston_layer struct.
2555 *
2556 * \param compositor The compositor instance
2557 * \param layer The layer to initialize
2558 */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002559WL_EXPORT void
Quentin Glidic82681572016-12-17 13:40:51 +01002560weston_layer_init(struct weston_layer *layer,
2561 struct weston_compositor *compositor)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002562{
Quentin Glidic82681572016-12-17 13:40:51 +01002563 layer->compositor = compositor;
2564 wl_list_init(&layer->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002565 wl_list_init(&layer->view_list.link);
2566 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002567 weston_layer_set_mask_infinite(layer);
Quentin Glidic82681572016-12-17 13:40:51 +01002568}
2569
2570/** Sets the position of the layer in the layer list. The layer will be placed
2571 * below any layer with the same position value, if any.
2572 * This function is safe to call if the layer is already on the list, but the
2573 * layer may be moved below other layers at the same position, if any.
2574 *
2575 * \param layer The layer to modify
2576 * \param position The position the layer will be placed at
2577 */
2578WL_EXPORT void
2579weston_layer_set_position(struct weston_layer *layer,
2580 enum weston_layer_position position)
2581{
2582 struct weston_layer *below;
2583
2584 wl_list_remove(&layer->link);
2585
2586 /* layer_list is ordered from top to bottom, the last layer being the
2587 * background with the smallest position value */
2588
2589 layer->position = position;
2590 wl_list_for_each_reverse(below, &layer->compositor->layer_list, link) {
2591 if (below->position >= layer->position) {
2592 wl_list_insert(&below->link, &layer->link);
2593 return;
2594 }
2595 }
2596 wl_list_insert(&layer->compositor->layer_list, &layer->link);
2597}
2598
2599/** Hide a layer by taking it off the layer list.
2600 * This function is safe to call if the layer is not on the list.
2601 *
2602 * \param layer The layer to hide
2603 */
2604WL_EXPORT void
2605weston_layer_unset_position(struct weston_layer *layer)
2606{
2607 wl_list_remove(&layer->link);
2608 wl_list_init(&layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002609}
2610
2611WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002612weston_layer_set_mask(struct weston_layer *layer,
2613 int x, int y, int width, int height)
2614{
2615 struct weston_view *view;
2616
2617 layer->mask.x1 = x;
2618 layer->mask.x2 = x + width;
2619 layer->mask.y1 = y;
2620 layer->mask.y2 = y + height;
2621
2622 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2623 weston_view_geometry_dirty(view);
2624 }
2625}
2626
2627WL_EXPORT void
2628weston_layer_set_mask_infinite(struct weston_layer *layer)
2629{
2630 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2631 UINT32_MAX, UINT32_MAX);
2632}
2633
2634WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002635weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002636{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002637 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002638 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002639
Bryce Harrington08976ac2016-08-30 12:05:16 -07002640 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2641 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002642 return;
2643
Pekka Paalanenb5026542014-11-12 15:09:24 +02002644 if (!output->repaint_needed)
2645 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2646
Kristian Høgsbergef044142011-06-21 15:02:12 -04002647 loop = wl_display_get_event_loop(compositor->wl_display);
Daniel Stone09a97e22017-03-01 11:34:06 +00002648 output->repaint_needed = true;
Daniel Stone05df8c12017-03-03 16:59:42 +00002649
2650 /* If we already have a repaint scheduled for our idle handler,
2651 * no need to set it again. If the repaint has been called but
2652 * not finished, then weston_output_finish_frame() will notice
2653 * that a repaint is needed and schedule one. */
2654 if (output->repaint_status != REPAINT_NOT_SCHEDULED)
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002655 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002656
Daniel Stone05df8c12017-03-03 16:59:42 +00002657 output->repaint_status = REPAINT_BEGIN_FROM_IDLE;
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002658 wl_event_loop_add_idle(loop, idle_repaint, output);
Pekka Paalanenb5026542014-11-12 15:09:24 +02002659 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002660}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002661
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002662WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002663weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2664{
2665 struct weston_output *output;
2666
2667 wl_list_for_each(output, &compositor->output_list, link)
2668 weston_output_schedule_repaint(output);
2669}
2670
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002671static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002672surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002673{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002674 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002675}
2676
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002677static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002678surface_attach(struct wl_client *client,
2679 struct wl_resource *resource,
2680 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2681{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002682 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002683 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002684
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002685 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002686 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002687 if (buffer == NULL) {
2688 wl_client_post_no_memory(client);
2689 return;
2690 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002691 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002692
Pekka Paalanende685b82012-12-04 15:58:12 +02002693 /* Attach, attach, without commit in between does not send
2694 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002695 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002696
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002697 surface->pending.sx = sx;
2698 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002699 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002700}
2701
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002702static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002703surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002704 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002705 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002706{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002707 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002708
Derek Foreman57e92ed2015-11-17 14:11:35 -06002709 if (width <= 0 || height <= 0)
2710 return;
2711
Derek Foreman152254b2015-11-26 14:17:48 -06002712 pixman_region32_union_rect(&surface->pending.damage_surface,
2713 &surface->pending.damage_surface,
2714 x, y, width, height);
2715}
2716
2717static void
2718surface_damage_buffer(struct wl_client *client,
2719 struct wl_resource *resource,
2720 int32_t x, int32_t y, int32_t width, int32_t height)
2721{
2722 struct weston_surface *surface = wl_resource_get_user_data(resource);
2723
2724 if (width <= 0 || height <= 0)
2725 return;
2726
2727 pixman_region32_union_rect(&surface->pending.damage_buffer,
2728 &surface->pending.damage_buffer,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002729 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002730}
2731
Kristian Høgsberg33418202011-08-16 23:01:28 -04002732static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002733destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002734{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002735 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002736
2737 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002738 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002739}
2740
2741static void
2742surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002743 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002744{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002745 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002746 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002747
2748 cb = malloc(sizeof *cb);
2749 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002750 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002751 return;
2752 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002753
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002754 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2755 callback);
2756 if (cb->resource == NULL) {
2757 free(cb);
2758 wl_resource_post_no_memory(resource);
2759 return;
2760 }
2761
Jason Ekstranda85118c2013-06-27 20:17:02 -05002762 wl_resource_set_implementation(cb->resource, NULL, cb,
2763 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002764
Pekka Paalanenbc106382012-10-10 12:49:31 +03002765 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002766}
2767
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002768static void
2769surface_set_opaque_region(struct wl_client *client,
2770 struct wl_resource *resource,
2771 struct wl_resource *region_resource)
2772{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002773 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002774 struct weston_region *region;
2775
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002776 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002777 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002778 pixman_region32_copy(&surface->pending.opaque,
2779 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002780 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002781 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002782 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002783}
2784
2785static void
2786surface_set_input_region(struct wl_client *client,
2787 struct wl_resource *resource,
2788 struct wl_resource *region_resource)
2789{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002790 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002791 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002792
2793 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002794 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002795 pixman_region32_copy(&surface->pending.input,
2796 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002797 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002798 pixman_region32_fini(&surface->pending.input);
2799 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002800 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002801}
2802
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002803/* Cause damage to this sub-surface and all its children.
2804 *
2805 * This is useful when there are state changes that need an implicit
2806 * damage, e.g. a z-order change.
2807 */
2808static void
2809weston_surface_damage_subsurfaces(struct weston_subsurface *sub)
2810{
2811 struct weston_subsurface *child;
2812
2813 weston_surface_damage(sub->surface);
2814 sub->reordered = false;
2815
2816 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
2817 if (child != sub)
2818 weston_surface_damage_subsurfaces(child);
2819}
2820
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002821static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002822weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002823{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002824 struct weston_subsurface *sub;
2825
2826 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2827 parent_link_pending) {
2828 wl_list_remove(&sub->parent_link);
2829 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01002830
2831 if (sub->reordered)
2832 weston_surface_damage_subsurfaces(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002833 }
2834}
2835
2836static void
Pekka Paalanen04baea52016-04-26 15:50:58 +03002837weston_surface_build_buffer_matrix(const struct weston_surface *surface,
Jason Ekstrand1e059042014-10-16 10:55:19 -05002838 struct weston_matrix *matrix)
2839{
Pekka Paalanen04baea52016-04-26 15:50:58 +03002840 const struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jason Ekstrand1e059042014-10-16 10:55:19 -05002841 double src_width, src_height, dest_width, dest_height;
2842
2843 weston_matrix_init(matrix);
2844
2845 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2846 src_width = surface->width_from_buffer;
2847 src_height = surface->height_from_buffer;
2848 } else {
2849 src_width = wl_fixed_to_double(vp->buffer.src_width);
2850 src_height = wl_fixed_to_double(vp->buffer.src_height);
2851 }
2852
2853 if (vp->surface.width == -1) {
2854 dest_width = src_width;
2855 dest_height = src_height;
2856 } else {
2857 dest_width = vp->surface.width;
2858 dest_height = vp->surface.height;
2859 }
2860
2861 if (src_width != dest_width || src_height != dest_height)
2862 weston_matrix_scale(matrix,
2863 src_width / dest_width,
2864 src_height / dest_height, 1);
2865
2866 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2867 weston_matrix_translate(matrix,
2868 wl_fixed_to_double(vp->buffer.src_x),
2869 wl_fixed_to_double(vp->buffer.src_y),
2870 0);
2871
2872 switch (vp->buffer.transform) {
2873 case WL_OUTPUT_TRANSFORM_FLIPPED:
2874 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2875 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2876 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2877 weston_matrix_scale(matrix, -1, 1, 1);
2878 weston_matrix_translate(matrix,
2879 surface->width_from_buffer, 0, 0);
2880 break;
2881 }
2882
2883 switch (vp->buffer.transform) {
2884 default:
2885 case WL_OUTPUT_TRANSFORM_NORMAL:
2886 case WL_OUTPUT_TRANSFORM_FLIPPED:
2887 break;
2888 case WL_OUTPUT_TRANSFORM_90:
2889 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2890 weston_matrix_rotate_xy(matrix, 0, 1);
2891 weston_matrix_translate(matrix,
2892 surface->height_from_buffer, 0, 0);
2893 break;
2894 case WL_OUTPUT_TRANSFORM_180:
2895 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2896 weston_matrix_rotate_xy(matrix, -1, 0);
2897 weston_matrix_translate(matrix,
2898 surface->width_from_buffer,
2899 surface->height_from_buffer, 0);
2900 break;
2901 case WL_OUTPUT_TRANSFORM_270:
2902 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2903 weston_matrix_rotate_xy(matrix, 0, -1);
2904 weston_matrix_translate(matrix,
2905 0, surface->width_from_buffer, 0);
2906 break;
2907 }
2908
2909 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2910}
2911
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03002912/**
2913 * Compute a + b > c while being safe to overflows.
2914 */
2915static bool
2916fixed_sum_gt(wl_fixed_t a, wl_fixed_t b, wl_fixed_t c)
2917{
2918 return (int64_t)a + (int64_t)b > (int64_t)c;
2919}
2920
2921static bool
2922weston_surface_is_pending_viewport_source_valid(
2923 const struct weston_surface *surface)
2924{
2925 const struct weston_surface_state *pend = &surface->pending;
2926 const struct weston_buffer_viewport *vp = &pend->buffer_viewport;
2927 int width_from_buffer = 0;
2928 int height_from_buffer = 0;
2929 wl_fixed_t w;
2930 wl_fixed_t h;
2931
2932 /* If viewport source rect is not set, it is always ok. */
2933 if (vp->buffer.src_width == wl_fixed_from_int(-1))
2934 return true;
2935
2936 if (pend->newly_attached) {
2937 if (pend->buffer) {
2938 convert_size_by_transform_scale(&width_from_buffer,
2939 &height_from_buffer,
2940 pend->buffer->width,
2941 pend->buffer->height,
2942 vp->buffer.transform,
2943 vp->buffer.scale);
2944 } else {
2945 /* No buffer: viewport is irrelevant. */
2946 return true;
2947 }
2948 } else {
2949 width_from_buffer = surface->width_from_buffer;
2950 height_from_buffer = surface->height_from_buffer;
2951 }
2952
2953 assert((width_from_buffer == 0) == (height_from_buffer == 0));
2954 assert(width_from_buffer >= 0 && height_from_buffer >= 0);
2955
2956 /* No buffer: viewport is irrelevant. */
2957 if (width_from_buffer == 0 || height_from_buffer == 0)
2958 return true;
2959
2960 /* overflow checks for wl_fixed_from_int() */
2961 if (width_from_buffer > wl_fixed_to_int(INT32_MAX))
2962 return false;
2963 if (height_from_buffer > wl_fixed_to_int(INT32_MAX))
2964 return false;
2965
2966 w = wl_fixed_from_int(width_from_buffer);
2967 h = wl_fixed_from_int(height_from_buffer);
2968
2969 if (fixed_sum_gt(vp->buffer.src_x, vp->buffer.src_width, w))
2970 return false;
2971 if (fixed_sum_gt(vp->buffer.src_y, vp->buffer.src_height, h))
2972 return false;
2973
2974 return true;
2975}
2976
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03002977static bool
2978fixed_is_integer(wl_fixed_t v)
2979{
2980 return (v & 0xff) == 0;
2981}
2982
2983static bool
2984weston_surface_is_pending_viewport_dst_size_int(
2985 const struct weston_surface *surface)
2986{
2987 const struct weston_buffer_viewport *vp =
2988 &surface->pending.buffer_viewport;
2989
2990 if (vp->surface.width != -1) {
2991 assert(vp->surface.width > 0 && vp->surface.height > 0);
2992 return true;
2993 }
2994
2995 return fixed_is_integer(vp->buffer.src_width) &&
2996 fixed_is_integer(vp->buffer.src_height);
2997}
2998
Derek Foreman152254b2015-11-26 14:17:48 -06002999/* Translate pending damage in buffer co-ordinates to surface
3000 * co-ordinates and union it with a pixman_region32_t.
3001 * This should only be called after the buffer is attached.
3002 */
3003static void
3004apply_damage_buffer(pixman_region32_t *dest,
3005 struct weston_surface *surface,
3006 struct weston_surface_state *state)
3007{
3008 struct weston_buffer *buffer = surface->buffer_ref.buffer;
3009
3010 /* wl_surface.damage_buffer needs to be clipped to the buffer,
3011 * translated into surface co-ordinates and unioned with
3012 * any other surface damage.
3013 * None of this makes sense if there is no buffer though.
3014 */
3015 if (buffer && pixman_region32_not_empty(&state->damage_buffer)) {
3016 pixman_region32_t buffer_damage;
3017
3018 pixman_region32_intersect_rect(&state->damage_buffer,
3019 &state->damage_buffer,
3020 0, 0, buffer->width,
3021 buffer->height);
3022 pixman_region32_init(&buffer_damage);
3023 weston_matrix_transform_region(&buffer_damage,
3024 &surface->buffer_to_surface_matrix,
3025 &state->damage_buffer);
3026 pixman_region32_union(dest, dest, &buffer_damage);
3027 pixman_region32_fini(&buffer_damage);
3028 }
3029 /* We should clear this on commit even if there was no buffer */
3030 pixman_region32_clear(&state->damage_buffer);
3031}
3032
Jason Ekstrand1e059042014-10-16 10:55:19 -05003033static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05003034weston_surface_commit_state(struct weston_surface *surface,
3035 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003036{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003037 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003038 pixman_region32_t opaque;
3039
Alexander Larsson4ea95522013-05-22 14:41:37 +02003040 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02003041 /* wl_surface.set_buffer_scale */
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03003042 /* wp_viewport.set_source */
3043 /* wp_viewport.set_destination */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003044 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003045
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003046 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003047 if (state->newly_attached)
3048 weston_surface_attach(surface, state->buffer);
3049 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01003050
Jason Ekstrand1e059042014-10-16 10:55:19 -05003051 weston_surface_build_buffer_matrix(surface,
3052 &surface->surface_to_buffer_matrix);
3053 weston_matrix_invert(&surface->buffer_to_surface_matrix,
3054 &surface->surface_to_buffer_matrix);
3055
Jason Ekstrand7b982072014-05-20 14:33:03 -05003056 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003057 weston_surface_update_size(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003058 if (surface->committed)
3059 surface->committed(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003060 }
Giulio Camuffo184df502013-02-21 11:29:21 +01003061
Jason Ekstrand7b982072014-05-20 14:33:03 -05003062 state->sx = 0;
3063 state->sy = 0;
3064 state->newly_attached = 0;
3065 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03003066
Derek Foreman152254b2015-11-26 14:17:48 -06003067 /* wl_surface.damage and wl_surface.damage_buffer */
Pekka Paalanenb5026542014-11-12 15:09:24 +02003068 if (weston_timeline_enabled_ &&
Derek Foreman152254b2015-11-26 14:17:48 -06003069 (pixman_region32_not_empty(&state->damage_surface) ||
3070 pixman_region32_not_empty(&state->damage_buffer)))
Pekka Paalanenb5026542014-11-12 15:09:24 +02003071 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Derek Foreman152254b2015-11-26 14:17:48 -06003072
Pekka Paalanen8e159182012-10-10 12:49:25 +03003073 pixman_region32_union(&surface->damage, &surface->damage,
Derek Foreman152254b2015-11-26 14:17:48 -06003074 &state->damage_surface);
3075
3076 apply_damage_buffer(&surface->damage, surface, state);
3077
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05003078 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07003079 0, 0, surface->width, surface->height);
Derek Foreman152254b2015-11-26 14:17:48 -06003080 pixman_region32_clear(&state->damage_surface);
Pekka Paalanen512dde82012-10-10 12:49:27 +03003081
3082 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003083 pixman_region32_init(&opaque);
3084 pixman_region32_intersect_rect(&opaque, &state->opaque,
3085 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003086
3087 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
3088 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003089 wl_list_for_each(view, &surface->views, surface_link)
3090 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02003091 }
3092
3093 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003094
3095 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05003096 pixman_region32_intersect_rect(&surface->input, &state->input,
3097 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003098
Pekka Paalanenbc106382012-10-10 12:49:31 +03003099 /* wl_surface.frame */
3100 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05003101 &state->frame_callback_list);
3102 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003103
3104 /* XXX:
3105 * What should happen with a feedback request, if there
3106 * is no wl_buffer attached for this commit?
3107 */
3108
3109 /* presentation.feedback */
3110 wl_list_insert_list(&surface->feedback_list,
3111 &state->feedback_list);
3112 wl_list_init(&state->feedback_list);
Jonas Ådahl5d9ca272016-07-22 17:48:03 +08003113
3114 wl_signal_emit(&surface->commit_signal, surface);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003115}
3116
3117static void
3118weston_surface_commit(struct weston_surface *surface)
3119{
3120 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03003121
Pekka Paalanene67858b2013-04-25 13:57:42 +03003122 weston_surface_commit_subsurface_order(surface);
3123
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03003124 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003125}
3126
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003127static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003128weston_subsurface_commit(struct weston_subsurface *sub);
3129
3130static void
3131weston_subsurface_parent_commit(struct weston_subsurface *sub,
3132 int parent_is_synchronized);
3133
3134static void
3135surface_commit(struct wl_client *client, struct wl_resource *resource)
3136{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003137 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003138 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
3139
Pekka Paalanend9aae9c2016-04-26 13:46:38 +03003140 if (!weston_surface_is_pending_viewport_source_valid(surface)) {
3141 assert(surface->viewport_resource);
3142
3143 wl_resource_post_error(surface->viewport_resource,
3144 WP_VIEWPORT_ERROR_OUT_OF_BUFFER,
3145 "wl_surface@%d has viewport source outside buffer",
3146 wl_resource_get_id(resource));
3147 return;
3148 }
3149
Pekka Paalanenbb32ccc2016-04-26 14:28:28 +03003150 if (!weston_surface_is_pending_viewport_dst_size_int(surface)) {
3151 assert(surface->viewport_resource);
3152
3153 wl_resource_post_error(surface->viewport_resource,
3154 WP_VIEWPORT_ERROR_BAD_SIZE,
3155 "wl_surface@%d viewport dst size not integer",
3156 wl_resource_get_id(resource));
3157 return;
3158 }
3159
Pekka Paalanene67858b2013-04-25 13:57:42 +03003160 if (sub) {
3161 weston_subsurface_commit(sub);
3162 return;
3163 }
3164
3165 weston_surface_commit(surface);
3166
3167 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
3168 if (sub->surface != surface)
3169 weston_subsurface_parent_commit(sub, 0);
3170 }
3171}
3172
3173static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003174surface_set_buffer_transform(struct wl_client *client,
3175 struct wl_resource *resource, int transform)
3176{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003177 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003178
Jonny Lamba55f1392014-05-30 12:07:15 +02003179 /* if wl_output.transform grows more members this will need to be updated. */
3180 if (transform < 0 ||
3181 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
3182 wl_resource_post_error(resource,
3183 WL_SURFACE_ERROR_INVALID_TRANSFORM,
3184 "buffer transform must be a valid transform "
3185 "('%d' specified)", transform);
3186 return;
3187 }
3188
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003189 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003190 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003191}
3192
Alexander Larsson4ea95522013-05-22 14:41:37 +02003193static void
3194surface_set_buffer_scale(struct wl_client *client,
3195 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003196 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003197{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003198 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003199
Jonny Lamba55f1392014-05-30 12:07:15 +02003200 if (scale < 1) {
3201 wl_resource_post_error(resource,
3202 WL_SURFACE_ERROR_INVALID_SCALE,
3203 "buffer scale must be at least one "
3204 "('%d' specified)", scale);
3205 return;
3206 }
3207
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003208 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003209 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003210}
3211
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003212static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003213 surface_destroy,
3214 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04003215 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003216 surface_frame,
3217 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03003218 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02003219 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02003220 surface_set_buffer_transform,
Derek Foreman152254b2015-11-26 14:17:48 -06003221 surface_set_buffer_scale,
3222 surface_damage_buffer
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003223};
3224
3225static void
3226compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04003227 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003228{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04003229 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003230 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003231
Kristian Høgsberg18c93002012-01-27 11:58:31 -05003232 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003233 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04003234 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003235 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003236 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003237
Jason Ekstranda85118c2013-06-27 20:17:02 -05003238 surface->resource =
3239 wl_resource_create(client, &wl_surface_interface,
3240 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003241 if (surface->resource == NULL) {
3242 weston_surface_destroy(surface);
3243 wl_resource_post_no_memory(resource);
3244 return;
3245 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003246 wl_resource_set_implementation(surface->resource, &surface_interface,
3247 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003248
3249 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003250}
3251
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003252static void
3253destroy_region(struct wl_resource *resource)
3254{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003255 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003256
3257 pixman_region32_fini(&region->region);
3258 free(region);
3259}
3260
3261static void
3262region_destroy(struct wl_client *client, struct wl_resource *resource)
3263{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04003264 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003265}
3266
3267static void
3268region_add(struct wl_client *client, struct wl_resource *resource,
3269 int32_t x, int32_t y, int32_t width, int32_t height)
3270{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003271 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003272
3273 pixman_region32_union_rect(&region->region, &region->region,
3274 x, y, width, height);
3275}
3276
3277static void
3278region_subtract(struct wl_client *client, struct wl_resource *resource,
3279 int32_t x, int32_t y, int32_t width, int32_t height)
3280{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05003281 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003282 pixman_region32_t rect;
3283
3284 pixman_region32_init_rect(&rect, x, y, width, height);
3285 pixman_region32_subtract(&region->region, &region->region, &rect);
3286 pixman_region32_fini(&rect);
3287}
3288
3289static const struct wl_region_interface region_interface = {
3290 region_destroy,
3291 region_add,
3292 region_subtract
3293};
3294
3295static void
3296compositor_create_region(struct wl_client *client,
3297 struct wl_resource *resource, uint32_t id)
3298{
3299 struct weston_region *region;
3300
3301 region = malloc(sizeof *region);
3302 if (region == NULL) {
3303 wl_resource_post_no_memory(resource);
3304 return;
3305 }
3306
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003307 pixman_region32_init(&region->region);
3308
Jason Ekstranda85118c2013-06-27 20:17:02 -05003309 region->resource =
3310 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003311 if (region->resource == NULL) {
3312 free(region);
3313 wl_resource_post_no_memory(resource);
3314 return;
3315 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05003316 wl_resource_set_implementation(region->resource, &region_interface,
3317 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003318}
3319
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003320static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003321 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05003322 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05003323};
3324
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003325static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003326weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
3327{
3328 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003329
Jason Ekstrand7b982072014-05-20 14:33:03 -05003330 weston_surface_commit_state(surface, &sub->cached);
3331 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003332
3333 weston_surface_commit_subsurface_order(surface);
3334
3335 weston_surface_schedule_repaint(surface);
3336
Jason Ekstrand7b982072014-05-20 14:33:03 -05003337 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003338}
3339
3340static void
3341weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
3342{
3343 struct weston_surface *surface = sub->surface;
3344
3345 /*
3346 * If this commit would cause the surface to move by the
3347 * attach(dx, dy) parameters, the old damage region must be
3348 * translated to correspond to the new surface coordinate system
Chris Michael062edf22015-11-26 11:30:00 -05003349 * origin.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003350 */
Derek Foreman152254b2015-11-26 14:17:48 -06003351 pixman_region32_translate(&sub->cached.damage_surface,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003352 -surface->pending.sx, -surface->pending.sy);
Derek Foreman152254b2015-11-26 14:17:48 -06003353 pixman_region32_union(&sub->cached.damage_surface,
3354 &sub->cached.damage_surface,
3355 &surface->pending.damage_surface);
3356 pixman_region32_clear(&surface->pending.damage_surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003357
3358 if (surface->pending.newly_attached) {
3359 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003360 weston_surface_state_set_buffer(&sub->cached,
3361 surface->pending.buffer);
3362 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003363 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003364 weston_presentation_feedback_discard_list(
3365 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003366 }
3367 sub->cached.sx += surface->pending.sx;
3368 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003369
Derek Foreman152254b2015-11-26 14:17:48 -06003370 apply_damage_buffer(&sub->cached.damage_surface, surface, &surface->pending);
3371
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003372 sub->cached.buffer_viewport.changed |=
3373 surface->pending.buffer_viewport.changed;
3374 sub->cached.buffer_viewport.buffer =
3375 surface->pending.buffer_viewport.buffer;
3376 sub->cached.buffer_viewport.surface =
3377 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003378
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003379 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003380
3381 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3382
3383 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3384
3385 wl_list_insert_list(&sub->cached.frame_callback_list,
3386 &surface->pending.frame_callback_list);
3387 wl_list_init(&surface->pending.frame_callback_list);
3388
Pekka Paalanen133e4392014-09-23 22:08:46 -04003389 wl_list_insert_list(&sub->cached.feedback_list,
3390 &surface->pending.feedback_list);
3391 wl_list_init(&surface->pending.feedback_list);
3392
Jason Ekstrand7b982072014-05-20 14:33:03 -05003393 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003394}
3395
Derek Foreman280e7dd2014-10-03 13:13:42 -05003396static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003397weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3398{
3399 while (sub) {
3400 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003401 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003402
3403 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003404 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003405
3406 sub = weston_surface_to_subsurface(sub->parent);
3407 }
3408
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003409 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003410}
3411
3412static void
3413weston_subsurface_commit(struct weston_subsurface *sub)
3414{
3415 struct weston_surface *surface = sub->surface;
3416 struct weston_subsurface *tmp;
3417
3418 /* Recursive check for effectively synchronized. */
3419 if (weston_subsurface_is_synchronized(sub)) {
3420 weston_subsurface_commit_to_cache(sub);
3421 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003422 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003423 /* flush accumulated state from cache */
3424 weston_subsurface_commit_to_cache(sub);
3425 weston_subsurface_commit_from_cache(sub);
3426 } else {
3427 weston_surface_commit(surface);
3428 }
3429
3430 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3431 if (tmp->surface != surface)
3432 weston_subsurface_parent_commit(tmp, 0);
3433 }
3434 }
3435}
3436
3437static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003438weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003439{
3440 struct weston_surface *surface = sub->surface;
3441 struct weston_subsurface *tmp;
3442
Pekka Paalanene67858b2013-04-25 13:57:42 +03003443 /* From now on, commit_from_cache the whole sub-tree, regardless of
3444 * the synchronized mode of each child. This sub-surface or some
3445 * of its ancestors were synchronized, so we are synchronized
3446 * all the way down.
3447 */
3448
Jason Ekstrand7b982072014-05-20 14:33:03 -05003449 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003450 weston_subsurface_commit_from_cache(sub);
3451
3452 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3453 if (tmp->surface != surface)
3454 weston_subsurface_parent_commit(tmp, 1);
3455 }
3456}
3457
3458static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003459weston_subsurface_parent_commit(struct weston_subsurface *sub,
3460 int parent_is_synchronized)
3461{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003462 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003463 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003464 wl_list_for_each(view, &sub->surface->views, surface_link)
3465 weston_view_set_position(view,
3466 sub->position.x,
3467 sub->position.y);
3468
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003469 sub->position.set = 0;
3470 }
3471
3472 if (parent_is_synchronized || sub->synchronized)
3473 weston_subsurface_synchronized_commit(sub);
3474}
3475
Pekka Paalanen8274d902014-08-06 19:36:51 +03003476static int
3477subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3478{
3479 return snprintf(buf, len, "sub-surface");
3480}
3481
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003482static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003483subsurface_committed(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003484{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003485 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003486
Jason Ekstranda7af7042013-10-12 22:38:11 -05003487 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003488 weston_view_set_position(view,
3489 view->geometry.x + dx,
3490 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003491
3492 /* No need to check parent mappedness, because if parent is not
3493 * mapped, parent is not in a visible layer, so this sub-surface
3494 * will not be drawn either.
3495 */
Armin Krezovićf8486c32016-06-30 06:04:28 +02003496
Pekka Paalanene67858b2013-04-25 13:57:42 +03003497 if (!weston_surface_is_mapped(surface)) {
Armin Krezovićf8486c32016-06-30 06:04:28 +02003498 surface->is_mapped = true;
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003499
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003500 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003501 * because that would call it also for the parent surface,
3502 * which might not be mapped yet. That would lead to
3503 * inconsistent state, where the window could never be
3504 * mapped.
3505 *
Armin Krezovićf8486c32016-06-30 06:04:28 +02003506 * Instead just force the is_mapped flag on, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003507 * weston_surface_is_mapped() return true, so that when the
3508 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003509 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003510 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03003511 }
3512}
3513
3514static struct weston_subsurface *
3515weston_surface_to_subsurface(struct weston_surface *surface)
3516{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003517 if (surface->committed == subsurface_committed)
3518 return surface->committed_private;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003519
3520 return NULL;
3521}
3522
Pekka Paalanen01388e22013-04-25 13:57:44 +03003523WL_EXPORT struct weston_surface *
3524weston_surface_get_main_surface(struct weston_surface *surface)
3525{
3526 struct weston_subsurface *sub;
3527
3528 while (surface && (sub = weston_surface_to_subsurface(surface)))
3529 surface = sub->parent;
3530
3531 return surface;
3532}
3533
Pekka Paalanen50b67472014-10-01 15:02:41 +03003534WL_EXPORT int
3535weston_surface_set_role(struct weston_surface *surface,
3536 const char *role_name,
3537 struct wl_resource *error_resource,
3538 uint32_t error_code)
3539{
3540 assert(role_name);
3541
3542 if (surface->role_name == NULL ||
3543 surface->role_name == role_name ||
3544 strcmp(surface->role_name, role_name) == 0) {
3545 surface->role_name = role_name;
3546
3547 return 0;
3548 }
3549
3550 wl_resource_post_error(error_resource, error_code,
3551 "Cannot assign role %s to wl_surface@%d,"
3552 " already has role %s\n",
3553 role_name,
3554 wl_resource_get_id(surface->resource),
3555 surface->role_name);
3556 return -1;
3557}
3558
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02003559WL_EXPORT const char *
3560weston_surface_get_role(struct weston_surface *surface)
3561{
3562 return surface->role_name;
3563}
3564
Pekka Paalanen8274d902014-08-06 19:36:51 +03003565WL_EXPORT void
3566weston_surface_set_label_func(struct weston_surface *surface,
3567 int (*desc)(struct weston_surface *,
3568 char *, size_t))
3569{
3570 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003571 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003572}
3573
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003574/** Get the size of surface contents
3575 *
3576 * \param surface The surface to query.
3577 * \param width Returns the width of raw contents.
3578 * \param height Returns the height of raw contents.
3579 *
3580 * Retrieves the raw surface content size in pixels for the given surface.
3581 * This is the whole content size in buffer pixels. If the surface
3582 * has no content or the renderer does not implement this feature,
3583 * zeroes are returned.
3584 *
3585 * This function is used to determine the buffer size needed for
3586 * a weston_surface_copy_content() call.
3587 */
3588WL_EXPORT void
3589weston_surface_get_content_size(struct weston_surface *surface,
3590 int *width, int *height)
3591{
3592 struct weston_renderer *rer = surface->compositor->renderer;
3593
3594 if (!rer->surface_get_content_size) {
3595 *width = 0;
3596 *height = 0;
3597 return;
3598 }
3599
3600 rer->surface_get_content_size(surface, width, height);
3601}
3602
Quentin Glidic248dd102016-08-12 10:41:34 +02003603/** Get the bounding box of a surface and its subsurfaces
3604 *
3605 * \param surface The surface to query.
3606 * \return The bounding box relative to the surface origin.
3607 *
3608 */
3609WL_EXPORT struct weston_geometry
3610weston_surface_get_bounding_box(struct weston_surface *surface)
3611{
3612 pixman_region32_t region;
3613 pixman_box32_t *box;
3614 struct weston_subsurface *subsurface;
3615
3616 pixman_region32_init_rect(&region,
3617 0, 0,
3618 surface->width, surface->height);
3619
3620 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link)
3621 pixman_region32_union_rect(&region, &region,
3622 subsurface->position.x,
3623 subsurface->position.y,
3624 subsurface->surface->width,
3625 subsurface->surface->height);
3626
3627 box = pixman_region32_extents(&region);
3628 struct weston_geometry geometry = {
3629 .x = box->x1,
3630 .y = box->y1,
3631 .width = box->x2 - box->x1,
3632 .height = box->y2 - box->y1,
3633 };
3634
3635 pixman_region32_fini(&region);
3636
3637 return geometry;
3638}
3639
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003640/** Copy surface contents to system memory.
3641 *
3642 * \param surface The surface to copy from.
3643 * \param target Pointer to the target memory buffer.
3644 * \param size Size of the target buffer in bytes.
3645 * \param src_x X location on contents to copy from.
3646 * \param src_y Y location on contents to copy from.
3647 * \param width Width in pixels of the area to copy.
3648 * \param height Height in pixels of the area to copy.
3649 * \return 0 for success, -1 for failure.
3650 *
3651 * Surface contents are maintained by the renderer. They can be in a
3652 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3653 * else.
3654 *
3655 * Surface contents are copied into memory pointed to by target,
3656 * which has size bytes of space available. The target memory
3657 * may be larger than needed, but being smaller returns an error.
3658 * The extra bytes in target may or may not be written; their content is
3659 * unspecified. Size must be large enough to hold the image.
3660 *
3661 * The image in the target memory will be arranged in rows from
3662 * top to bottom, and pixels on a row from left to right. The pixel
3663 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3664 * width * 4.
3665 *
3666 * Parameters src_x and src_y define the upper-left corner in buffer
3667 * coordinates (pixels) to copy from. Parameters width and height
3668 * define the size of the area to copy in pixels.
3669 *
3670 * The rectangle defined by src_x, src_y, width, height must fit in
3671 * the surface contents. Otherwise an error is returned.
3672 *
3673 * Use surface_get_data_size to determine the content size; the
3674 * needed target buffer size and rectangle limits.
3675 *
3676 * CURRENT IMPLEMENTATION RESTRICTIONS:
3677 * - the machine must be little-endian due to Pixman formats.
3678 *
3679 * NOTE: Pixman formats are premultiplied.
3680 */
3681WL_EXPORT int
3682weston_surface_copy_content(struct weston_surface *surface,
3683 void *target, size_t size,
3684 int src_x, int src_y,
3685 int width, int height)
3686{
3687 struct weston_renderer *rer = surface->compositor->renderer;
3688 int cw, ch;
3689 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3690
3691 if (!rer->surface_copy_content)
3692 return -1;
3693
3694 weston_surface_get_content_size(surface, &cw, &ch);
3695
3696 if (src_x < 0 || src_y < 0)
3697 return -1;
3698
3699 if (width <= 0 || height <= 0)
3700 return -1;
3701
3702 if (src_x + width > cw || src_y + height > ch)
3703 return -1;
3704
3705 if (width * bytespp * height > size)
3706 return -1;
3707
3708 return rer->surface_copy_content(surface, target, size,
3709 src_x, src_y, width, height);
3710}
3711
Pekka Paalanene67858b2013-04-25 13:57:42 +03003712static void
3713subsurface_set_position(struct wl_client *client,
3714 struct wl_resource *resource, int32_t x, int32_t y)
3715{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003716 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003717
3718 if (!sub)
3719 return;
3720
3721 sub->position.x = x;
3722 sub->position.y = y;
3723 sub->position.set = 1;
3724}
3725
3726static struct weston_subsurface *
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003727subsurface_find_sibling(struct weston_subsurface *sub,
3728 struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003729{
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003730 struct weston_surface *parent = sub->parent;
3731 struct weston_subsurface *sibling;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003732
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003733 wl_list_for_each(sibling, &parent->subsurface_list, parent_link) {
3734 if (sibling->surface == surface && sibling != sub)
3735 return sibling;
3736 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003737
3738 return NULL;
3739}
3740
3741static struct weston_subsurface *
3742subsurface_sibling_check(struct weston_subsurface *sub,
3743 struct weston_surface *surface,
3744 const char *request)
3745{
3746 struct weston_subsurface *sibling;
3747
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003748 sibling = subsurface_find_sibling(sub, surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003749 if (!sibling) {
3750 wl_resource_post_error(sub->resource,
3751 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3752 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003753 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003754 return NULL;
3755 }
3756
Arnaud Vracb8c16c92016-06-08 18:37:57 +02003757 assert(sibling->parent == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003758
3759 return sibling;
3760}
3761
3762static void
3763subsurface_place_above(struct wl_client *client,
3764 struct wl_resource *resource,
3765 struct wl_resource *sibling_resource)
3766{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003767 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003768 struct weston_surface *surface =
3769 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003770 struct weston_subsurface *sibling;
3771
3772 if (!sub)
3773 return;
3774
3775 sibling = subsurface_sibling_check(sub, surface, "place_above");
3776 if (!sibling)
3777 return;
3778
3779 wl_list_remove(&sub->parent_link_pending);
3780 wl_list_insert(sibling->parent_link_pending.prev,
3781 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003782
3783 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003784}
3785
3786static void
3787subsurface_place_below(struct wl_client *client,
3788 struct wl_resource *resource,
3789 struct wl_resource *sibling_resource)
3790{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003791 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003792 struct weston_surface *surface =
3793 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003794 struct weston_subsurface *sibling;
3795
3796 if (!sub)
3797 return;
3798
3799 sibling = subsurface_sibling_check(sub, surface, "place_below");
3800 if (!sibling)
3801 return;
3802
3803 wl_list_remove(&sub->parent_link_pending);
3804 wl_list_insert(&sibling->parent_link_pending,
3805 &sub->parent_link_pending);
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +01003806
3807 sub->reordered = true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003808}
3809
3810static void
3811subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3812{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003813 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003814
3815 if (sub)
3816 sub->synchronized = 1;
3817}
3818
3819static void
3820subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3821{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003822 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003823
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003824 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003825 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003826
3827 /* If sub became effectively desynchronized, flush. */
3828 if (!weston_subsurface_is_synchronized(sub))
3829 weston_subsurface_synchronized_commit(sub);
3830 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003831}
3832
3833static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003834weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3835{
3836 wl_list_remove(&sub->parent_link);
3837 wl_list_remove(&sub->parent_link_pending);
3838 wl_list_remove(&sub->parent_destroy_listener.link);
3839 sub->parent = NULL;
3840}
3841
3842static void
3843weston_subsurface_destroy(struct weston_subsurface *sub);
3844
3845static void
3846subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3847{
3848 struct weston_subsurface *sub =
3849 container_of(listener, struct weston_subsurface,
3850 surface_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003851 assert(data == sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003852
3853 /* The protocol object (wl_resource) is left inert. */
3854 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003855 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003856
3857 weston_subsurface_destroy(sub);
3858}
3859
3860static void
3861subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3862{
3863 struct weston_subsurface *sub =
3864 container_of(listener, struct weston_subsurface,
3865 parent_destroy_listener);
Pekka Paalanenca790762015-04-17 14:23:38 +03003866 assert(data == sub->parent);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003867 assert(sub->surface != sub->parent);
3868
3869 if (weston_surface_is_mapped(sub->surface))
3870 weston_surface_unmap(sub->surface);
3871
3872 weston_subsurface_unlink_parent(sub);
3873}
3874
3875static void
3876subsurface_resource_destroy(struct wl_resource *resource)
3877{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003878 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003879
3880 if (sub)
3881 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003882}
3883
3884static void
3885subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3886{
3887 wl_resource_destroy(resource);
3888}
3889
3890static void
3891weston_subsurface_link_parent(struct weston_subsurface *sub,
3892 struct weston_surface *parent)
3893{
3894 sub->parent = parent;
3895 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003896 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003897 &sub->parent_destroy_listener);
3898
3899 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3900 wl_list_insert(&parent->subsurface_list_pending,
3901 &sub->parent_link_pending);
3902}
3903
3904static void
3905weston_subsurface_link_surface(struct weston_subsurface *sub,
3906 struct weston_surface *surface)
3907{
3908 sub->surface = surface;
3909 sub->surface_destroy_listener.notify =
3910 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003911 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003912 &sub->surface_destroy_listener);
3913}
3914
3915static void
3916weston_subsurface_destroy(struct weston_subsurface *sub)
3917{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003918 struct weston_view *view, *next;
3919
Pekka Paalanene67858b2013-04-25 13:57:42 +03003920 assert(sub->surface);
3921
3922 if (sub->resource) {
3923 assert(weston_surface_to_subsurface(sub->surface) == sub);
3924 assert(sub->parent_destroy_listener.notify ==
3925 subsurface_handle_parent_destroy);
3926
George Kiagiadakised04d382014-06-13 18:10:26 +02003927 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3928 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003929 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003930 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003931
Pekka Paalanene67858b2013-04-25 13:57:42 +03003932 if (sub->parent)
3933 weston_subsurface_unlink_parent(sub);
3934
Jason Ekstrand7b982072014-05-20 14:33:03 -05003935 weston_surface_state_fini(&sub->cached);
3936 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003937
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003938 sub->surface->committed = NULL;
3939 sub->surface->committed_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003940 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003941 } else {
3942 /* the dummy weston_subsurface for the parent itself */
3943 assert(sub->parent_destroy_listener.notify == NULL);
3944 wl_list_remove(&sub->parent_link);
3945 wl_list_remove(&sub->parent_link_pending);
3946 }
3947
3948 wl_list_remove(&sub->surface_destroy_listener.link);
3949 free(sub);
3950}
3951
3952static const struct wl_subsurface_interface subsurface_implementation = {
3953 subsurface_destroy,
3954 subsurface_set_position,
3955 subsurface_place_above,
3956 subsurface_place_below,
3957 subsurface_set_sync,
3958 subsurface_set_desync
3959};
3960
3961static struct weston_subsurface *
3962weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3963 struct weston_surface *parent)
3964{
3965 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003966 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003967
Bryce Harringtonde16d892014-11-20 22:21:57 -08003968 sub = zalloc(sizeof *sub);
3969 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003970 return NULL;
3971
Jason Ekstranda7af7042013-10-12 22:38:11 -05003972 wl_list_init(&sub->unused_views);
3973
Jason Ekstranda85118c2013-06-27 20:17:02 -05003974 sub->resource =
3975 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003976 if (!sub->resource) {
3977 free(sub);
3978 return NULL;
3979 }
3980
Jason Ekstranda85118c2013-06-27 20:17:02 -05003981 wl_resource_set_implementation(sub->resource,
3982 &subsurface_implementation,
3983 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003984 weston_subsurface_link_surface(sub, surface);
3985 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003986 weston_surface_state_init(&sub->cached);
3987 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003988 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003989
3990 return sub;
3991}
3992
3993/* Create a dummy subsurface for having the parent itself in its
3994 * sub-surface lists. Makes stacking order manipulation easy.
3995 */
3996static struct weston_subsurface *
3997weston_subsurface_create_for_parent(struct weston_surface *parent)
3998{
3999 struct weston_subsurface *sub;
4000
Bryce Harringtonde16d892014-11-20 22:21:57 -08004001 sub = zalloc(sizeof *sub);
4002 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004003 return NULL;
4004
4005 weston_subsurface_link_surface(sub, parent);
4006 sub->parent = parent;
4007 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
4008 wl_list_insert(&parent->subsurface_list_pending,
4009 &sub->parent_link_pending);
4010
4011 return sub;
4012}
4013
4014static void
4015subcompositor_get_subsurface(struct wl_client *client,
4016 struct wl_resource *resource,
4017 uint32_t id,
4018 struct wl_resource *surface_resource,
4019 struct wl_resource *parent_resource)
4020{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05004021 struct weston_surface *surface =
4022 wl_resource_get_user_data(surface_resource);
4023 struct weston_surface *parent =
4024 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004025 struct weston_subsurface *sub;
4026 static const char where[] = "get_subsurface: wl_subsurface@";
4027
4028 if (surface == parent) {
4029 wl_resource_post_error(resource,
4030 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4031 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004032 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004033 return;
4034 }
4035
4036 if (weston_surface_to_subsurface(surface)) {
4037 wl_resource_post_error(resource,
4038 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4039 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004040 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03004041 return;
4042 }
4043
Pekka Paalanen50b67472014-10-01 15:02:41 +03004044 if (weston_surface_set_role(surface, "wl_subsurface", resource,
4045 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03004046 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004047
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004048 if (weston_surface_get_main_surface(parent) == surface) {
4049 wl_resource_post_error(resource,
4050 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
4051 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05004052 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03004053 return;
4054 }
4055
Pekka Paalanene67858b2013-04-25 13:57:42 +03004056 /* make sure the parent is in its own list */
4057 if (wl_list_empty(&parent->subsurface_list)) {
4058 if (!weston_subsurface_create_for_parent(parent)) {
4059 wl_resource_post_no_memory(resource);
4060 return;
4061 }
4062 }
4063
4064 sub = weston_subsurface_create(id, surface, parent);
4065 if (!sub) {
4066 wl_resource_post_no_memory(resource);
4067 return;
4068 }
4069
Quentin Glidic2edc3d52016-08-12 10:41:33 +02004070 surface->committed = subsurface_committed;
4071 surface->committed_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03004072 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004073}
4074
4075static void
4076subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
4077{
4078 wl_resource_destroy(resource);
4079}
4080
4081static const struct wl_subcompositor_interface subcompositor_interface = {
4082 subcompositor_destroy,
4083 subcompositor_get_subsurface
4084};
4085
4086static void
4087bind_subcompositor(struct wl_client *client,
4088 void *data, uint32_t version, uint32_t id)
4089{
4090 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004091 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03004092
Jason Ekstranda85118c2013-06-27 20:17:02 -05004093 resource =
4094 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004095 if (resource == NULL) {
4096 wl_client_post_no_memory(client);
4097 return;
4098 }
4099 wl_resource_set_implementation(resource, &subcompositor_interface,
4100 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03004101}
4102
Bryce Harrington0795ece2016-08-30 12:04:26 -07004103/** Set a DPMS mode on all of the compositor's outputs
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004104 *
4105 * \param compositor The compositor instance
4106 * \param state The DPMS state the outputs will be set to
4107 */
Pekka Paalanene67858b2013-04-25 13:57:42 +03004108static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004109weston_compositor_dpms(struct weston_compositor *compositor,
4110 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004111{
4112 struct weston_output *output;
4113
Bryce Harrington08976ac2016-08-30 12:05:16 -07004114 wl_list_for_each(output, &compositor->output_list, link)
4115 if (output->set_dpms)
4116 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004117}
4118
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004119/** Restores the compositor to active status
4120 *
4121 * \param compositor The compositor instance
4122 *
4123 * If the compositor was in a sleeping mode, all outputs are powered
4124 * back on via DPMS. Otherwise if the compositor was inactive
4125 * (idle/locked, offscreen, or sleeping) then the compositor's wake
4126 * signal will fire.
4127 *
4128 * Restarts the idle timer.
4129 */
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004130WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004131weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004132{
Neil Roberts8b62e202013-09-30 13:14:47 +01004133 uint32_t old_state = compositor->state;
4134
4135 /* The state needs to be changed before emitting the wake
4136 * signal because that may try to schedule a repaint which
4137 * will not work if the compositor is still sleeping */
4138 compositor->state = WESTON_COMPOSITOR_ACTIVE;
4139
4140 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004141 case WESTON_COMPOSITOR_SLEEPING:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004142 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004143 case WESTON_COMPOSITOR_OFFSCREEN:
Daniel Stone893b9362016-11-08 15:47:09 +00004144 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004145 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004146 /* fall through */
4147 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004148 wl_event_source_timer_update(compositor->idle_source,
4149 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02004150 }
4151}
4152
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004153/** Turns off rendering and frame events for the compositor.
4154 *
4155 * \param compositor The compositor instance
4156 *
4157 * This is used for example to prevent further rendering while the
4158 * compositor is shutting down.
4159 *
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004160 * Stops the idle timer.
4161 */
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004162WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004163weston_compositor_offscreen(struct weston_compositor *compositor)
4164{
4165 switch (compositor->state) {
4166 case WESTON_COMPOSITOR_OFFSCREEN:
4167 return;
4168 case WESTON_COMPOSITOR_SLEEPING:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004169 default:
4170 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
4171 wl_event_source_timer_update(compositor->idle_source, 0);
4172 }
4173}
4174
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004175/** Powers down all attached output devices
4176 *
4177 * \param compositor The compositor instance
4178 *
4179 * Causes rendering to the outputs to cease, and no frame events to be
4180 * sent. Only powers down the outputs if the compositor is not already
4181 * in sleep mode.
4182 *
4183 * Stops the idle timer.
4184 */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004185WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004186weston_compositor_sleep(struct weston_compositor *compositor)
4187{
4188 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
4189 return;
4190
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004191 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004192 compositor->state = WESTON_COMPOSITOR_SLEEPING;
4193 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
4194}
4195
Bryce Harringtonc9626a32015-12-11 13:11:38 -08004196/** Sets compositor to idle mode
4197 *
4198 * \param data The compositor instance
4199 *
4200 * This is called when the idle timer fires. Once the compositor is in
4201 * idle mode it requires a wake action (e.g. via
4202 * weston_compositor_wake()) to restore it. The compositor's
4203 * idle_signal will be triggered when the idle event occurs.
4204 *
4205 * Idleness can be inhibited by setting the compositor's idle_inhibit
4206 * property.
4207 */
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004208static int
4209idle_handler(void *data)
4210{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004211 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004212
4213 if (compositor->idle_inhibit)
4214 return 1;
4215
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004216 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004217 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04004218
4219 return 1;
4220}
4221
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004222WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08004223weston_plane_init(struct weston_plane *plane,
4224 struct weston_compositor *ec,
4225 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004226{
4227 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004228 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004229 plane->x = x;
4230 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08004231 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004232
4233 /* Init the link so that the call to wl_list_remove() when releasing
4234 * the plane without ever stacking doesn't lead to a crash */
4235 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004236}
4237
4238WL_EXPORT void
4239weston_plane_release(struct weston_plane *plane)
4240{
Xiong Zhang97116532013-10-23 13:58:31 +08004241 struct weston_view *view;
4242
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004243 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02004244 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004245
Xiong Zhang97116532013-10-23 13:58:31 +08004246 wl_list_for_each(view, &plane->compositor->view_list, link) {
4247 if (view->plane == plane)
4248 view->plane = NULL;
4249 }
4250
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03004251 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004252}
4253
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004254WL_EXPORT void
4255weston_compositor_stack_plane(struct weston_compositor *ec,
4256 struct weston_plane *plane,
4257 struct weston_plane *above)
4258{
4259 if (above)
4260 wl_list_insert(above->link.prev, &plane->link);
4261 else
4262 wl_list_insert(&ec->plane_list, &plane->link);
4263}
4264
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004265static void
4266output_release(struct wl_client *client, struct wl_resource *resource)
4267{
4268 wl_resource_destroy(resource);
4269}
4270
4271static const struct wl_output_interface output_interface = {
4272 output_release,
4273};
4274
4275
Casey Dahlin9074db52012-04-19 22:50:09 -04004276static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004277{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004278 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04004279}
4280
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004281static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004282bind_output(struct wl_client *client,
4283 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05004284{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004285 struct weston_output *output = data;
4286 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004287 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004288
Jason Ekstranda85118c2013-06-27 20:17:02 -05004289 resource = wl_resource_create(client, &wl_output_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06004290 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004291 if (resource == NULL) {
4292 wl_client_post_no_memory(client);
4293 return;
4294 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04004295
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05004296 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Quentin Glidic4ef719c2016-07-05 20:44:33 +02004297 wl_resource_set_implementation(resource, &output_interface, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04004298
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004299 wl_output_send_geometry(resource,
4300 output->x,
4301 output->y,
4302 output->mm_width,
4303 output->mm_height,
4304 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04004305 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04004306 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004307 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004308 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02004309 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004310
4311 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05004312 wl_output_send_mode(resource,
4313 mode->flags,
4314 mode->width,
4315 mode->height,
4316 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04004317 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02004318
Jasper St. Pierre0013a292014-08-07 16:43:11 -04004319 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02004320 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05004321}
4322
David Fort0de859e2016-05-27 23:22:57 +02004323/* Move other outputs when one is resized so the space remains contiguous. */
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004324static void
David Fort0de859e2016-05-27 23:22:57 +02004325weston_compositor_reflow_outputs(struct weston_compositor *compositor,
4326 struct weston_output *resized_output, int delta_width)
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004327{
4328 struct weston_output *output;
David Fort0de859e2016-05-27 23:22:57 +02004329 bool start_resizing = false;
4330
4331 if (!delta_width)
4332 return;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004333
4334 wl_list_for_each(output, &compositor->output_list, link) {
David Fort0de859e2016-05-27 23:22:57 +02004335 if (output == resized_output) {
4336 start_resizing = true;
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004337 continue;
4338 }
4339
David Fort0de859e2016-05-27 23:22:57 +02004340 if (start_resizing) {
4341 weston_output_move(output, output->x + delta_width, output->y);
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02004342 output->dirty = 1;
4343 }
4344 }
4345}
4346
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004347WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07004348weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004349{
Scott Moreau850ca422012-05-21 15:21:25 -06004350 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05004351
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004352 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004353 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06004354
Scott Moreauccbf29d2012-02-22 14:21:41 -07004355 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06004356 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05004357 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004358 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004359 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01004360 weston_matrix_scale(&output->matrix, magnification,
4361 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004362 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004363
Jason Ekstrandfb23df72014-10-16 10:55:21 -05004364 switch (output->transform) {
4365 case WL_OUTPUT_TRANSFORM_FLIPPED:
4366 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4367 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4368 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4369 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4370 weston_matrix_scale(&output->matrix, -1, 1, 1);
4371 break;
4372 }
4373
4374 switch (output->transform) {
4375 default:
4376 case WL_OUTPUT_TRANSFORM_NORMAL:
4377 case WL_OUTPUT_TRANSFORM_FLIPPED:
4378 break;
4379 case WL_OUTPUT_TRANSFORM_90:
4380 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4381 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
4382 weston_matrix_rotate_xy(&output->matrix, 0, 1);
4383 break;
4384 case WL_OUTPUT_TRANSFORM_180:
4385 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
4386 weston_matrix_translate(&output->matrix,
4387 -output->width, -output->height, 0);
4388 weston_matrix_rotate_xy(&output->matrix, -1, 0);
4389 break;
4390 case WL_OUTPUT_TRANSFORM_270:
4391 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4392 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
4393 weston_matrix_rotate_xy(&output->matrix, 0, -1);
4394 break;
4395 }
4396
4397 if (output->current_scale != 1)
4398 weston_matrix_scale(&output->matrix,
4399 output->current_scale,
4400 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01004401
Scott Moreauccbf29d2012-02-22 14:21:41 -07004402 output->dirty = 0;
Derek Foremanc0023212015-03-24 11:36:13 -05004403
4404 weston_matrix_invert(&output->inverse_matrix, &output->matrix);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004405}
4406
Scott Moreau1bad5db2012-08-18 01:04:05 -06004407static void
Alexander Larsson0b135062013-05-28 16:23:36 +02004408weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004409{
4410 output->transform = transform;
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004411 output->native_scale = scale;
4412 output->current_scale = scale;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004413
Pekka Paalanen59987fa2016-04-26 15:50:59 +03004414 convert_size_by_transform_scale(&output->width, &output->height,
4415 output->current_mode->width,
4416 output->current_mode->height,
4417 transform, scale);
Alexander Larsson4ea95522013-05-22 14:41:37 +02004418}
4419
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004420static void
4421weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004422{
4423 output->x = x;
4424 output->y = y;
4425
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004426 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004427 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004428 output->width,
4429 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004430}
4431
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004432WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004433weston_output_move(struct weston_output *output, int x, int y)
4434{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004435 struct wl_resource *resource;
4436
4437 output->move_x = x - output->x;
4438 output->move_y = y - output->y;
4439
4440 if (output->move_x == 0 && output->move_y == 0)
4441 return;
4442
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004443 weston_output_init_geometry(output, x, y);
4444
4445 output->dirty = 1;
4446
4447 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004448 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004449
4450 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004451 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004452 wl_output_send_geometry(resource,
4453 output->x,
4454 output->y,
4455 output->mm_width,
4456 output->mm_height,
4457 output->subpixel,
4458 output->make,
4459 output->model,
4460 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004461
FORT David8a120692016-04-26 23:34:06 +02004462 if (wl_resource_get_version(resource) >= WL_OUTPUT_DONE_SINCE_VERSION)
Quanxian Wangb2c86362014-03-14 09:16:25 +08004463 wl_output_send_done(resource);
4464 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004465}
4466
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004467/** Signal that a pending output is taken into use.
4468 *
4469 * Removes the output from the pending list and adds it to the compositor's
4470 * list of enabled outputs. The output created signal is emitted.
Giulio Camuffob1147152015-05-06 21:41:57 +03004471 *
4472 * \param compositor The compositor instance.
4473 * \param output The output to be added.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004474 *
4475 * \internal
Giulio Camuffob1147152015-05-06 21:41:57 +03004476 */
4477WL_EXPORT void
4478weston_compositor_add_output(struct weston_compositor *compositor,
4479 struct weston_output *output)
4480{
Armin Krezoviće5403842016-08-05 15:28:29 +02004481 struct weston_view *view, *next;
4482
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004483 wl_list_remove(&output->link);
Giulio Camuffob1147152015-05-06 21:41:57 +03004484 wl_list_insert(compositor->output_list.prev, &output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004485
Giulio Camuffob1147152015-05-06 21:41:57 +03004486 wl_signal_emit(&compositor->output_created_signal, output);
Armin Krezoviće5403842016-08-05 15:28:29 +02004487
4488 wl_list_for_each_safe(view, next, &compositor->view_list, link)
4489 weston_view_geometry_dirty(view);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004490}
4491
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004492/** Transform device coordinates into global coordinates
4493 *
4494 * \param device_x[in] X coordinate in device units.
4495 * \param device_y[in] Y coordinate in device units.
4496 * \param x[out] X coordinate in the global space.
4497 * \param y[out] Y coordinate in the global space.
4498 *
4499 * Transforms coordinates from the device coordinate space
4500 * (physical pixel units) to the global coordinate space (logical pixel units).
4501 * This takes into account output transform and scale.
4502 *
4503 * \memberof weston_output
4504 * \internal
4505 */
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004506WL_EXPORT void
4507weston_output_transform_coordinate(struct weston_output *output,
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004508 double device_x, double device_y,
4509 double *x, double *y)
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004510{
Derek Foreman0f679412014-10-02 13:41:17 -05004511 struct weston_vector p = { {
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004512 device_x,
4513 device_y,
Derek Foreman0f679412014-10-02 13:41:17 -05004514 0.0,
4515 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004516
Derek Foreman67a18b92015-03-24 11:36:14 -05004517 weston_matrix_transform(&output->inverse_matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004518
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02004519 *x = p.f[0] / p.f[3];
4520 *y = p.f[1] / p.f[3];
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004521}
4522
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004523/** Undoes changes to an output done by weston_output_enable()
4524 *
4525 * \param output The weston_output object that needs the changes undone.
4526 *
4527 * Removes the repaint timer.
4528 * Destroys the Wayland global assigned to the output.
4529 * Destroys pixman regions allocated to the output.
4530 * Deallocates output's ID and updates compositor's output_id_pool.
4531 */
4532static void
4533weston_output_enable_undo(struct weston_output *output)
4534{
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004535 wl_global_destroy(output->global);
4536
4537 pixman_region32_fini(&output->region);
4538 pixman_region32_fini(&output->previous_damage);
4539 output->compositor->output_id_pool &= ~(1u << output->id);
4540
4541 output->enabled = false;
4542}
4543
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004544/** Removes output from compositor's list of enabled outputs
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004545 *
4546 * \param output The weston_output object that is being removed.
4547 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004548 * The following happens:
4549 *
4550 * - The output assignments of all views in the current scenegraph are
4551 * recomputed.
4552 *
4553 * - Presentation feedback is discarded.
4554 *
4555 * - Compositor is notified that outputs were changed and
4556 * applies the necessary changes to re-layout outputs.
4557 *
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004558 * - The output is put back in the pending outputs list.
4559 *
Pekka Paalanene6ac4fc2017-03-29 16:07:34 +03004560 * - Signal is emitted to notify all users of the weston_output
4561 * object that the output is being destroyed.
4562 *
4563 * - wl_output protocol objects referencing this weston_output
4564 * are made inert.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004565 *
4566 * \memberof weston_output
4567 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004568 */
4569static void
4570weston_compositor_remove_output(struct weston_output *output)
4571{
Pekka Paalanenbccda712017-03-29 16:16:04 +03004572 struct weston_compositor *compositor = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004573 struct wl_resource *resource;
4574 struct weston_view *view;
4575
4576 assert(output->destroying);
4577
Pekka Paalanenbccda712017-03-29 16:16:04 +03004578 wl_list_for_each(view, &compositor->view_list, link) {
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004579 if (view->output_mask & (1u << output->id))
4580 weston_view_assign_output(view);
4581 }
4582
4583 weston_presentation_feedback_discard_list(&output->feedback_list);
4584
Pekka Paalanenbccda712017-03-29 16:16:04 +03004585 weston_compositor_reflow_outputs(compositor, output, output->width);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004586
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004587 wl_list_remove(&output->link);
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004588 wl_list_insert(compositor->pending_output_list.prev, &output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004589
Pekka Paalanenbccda712017-03-29 16:16:04 +03004590 wl_signal_emit(&compositor->output_destroyed_signal, output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004591 wl_signal_emit(&output->destroy_signal, output);
4592
4593 wl_resource_for_each(resource, &output->resource_list) {
4594 wl_resource_set_destructor(resource, NULL);
4595 }
4596}
4597
4598/** Sets the output scale for a given output.
4599 *
4600 * \param output The weston_output object that the scale is set for.
4601 * \param scale Scale factor for the given output.
4602 *
4603 * It only supports setting scale for an output that
4604 * is not enabled and it can only be ran once.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004605 *
4606 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004607 */
4608WL_EXPORT void
4609weston_output_set_scale(struct weston_output *output,
4610 int32_t scale)
4611{
4612 /* We can only set scale on a disabled output */
4613 assert(!output->enabled);
4614
4615 /* We only want to set scale once */
4616 assert(!output->scale);
4617
4618 output->scale = scale;
4619}
4620
4621/** Sets the output transform for a given output.
4622 *
4623 * \param output The weston_output object that the transform is set for.
4624 * \param transform Transform value for the given output.
4625 *
4626 * It only supports setting transform for an output that is
4627 * not enabled and it can only be ran once.
4628 *
4629 * Refer to wl_output::transform section located at
4630 * https://wayland.freedesktop.org/docs/html/apa.html#protocol-spec-wl_output
4631 * for list of values that can be passed to this function.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004632 *
4633 * \memberof weston_output
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004634 */
4635WL_EXPORT void
4636weston_output_set_transform(struct weston_output *output,
4637 uint32_t transform)
4638{
4639 /* We can only set transform on a disabled output */
4640 assert(!output->enabled);
4641
4642 /* We only want to set transform once */
4643 assert(output->transform == UINT32_MAX);
4644
4645 output->transform = transform;
4646}
4647
4648/** Initializes a weston_output object with enough data so
4649 ** an output can be configured.
4650 *
4651 * \param output The weston_output object to initialize
4652 * \param compositor The compositor instance.
4653 *
4654 * Sets initial values for fields that are expected to be
4655 * configured either by compositors or backends.
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004656 *
4657 * \memberof weston_output
4658 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004659 */
4660WL_EXPORT void
Armin Krezović40087402016-09-30 14:11:12 +02004661weston_output_init(struct weston_output *output,
4662 struct weston_compositor *compositor)
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004663{
4664 output->compositor = compositor;
4665 output->destroying = 0;
4666
4667 /* Backends must set output->name */
4668 assert(output->name);
4669
4670 wl_list_init(&output->link);
4671
4672 output->enabled = false;
4673
4674 /* Add some (in)sane defaults which can be used
4675 * for checking if an output was properly configured
4676 */
4677 output->mm_width = 0;
4678 output->mm_height = 0;
4679 output->scale = 0;
4680 /* Can't use -1 on uint32_t and 0 is valid enum value */
4681 output->transform = UINT32_MAX;
4682}
4683
4684/** Adds weston_output object to pending output list.
4685 *
4686 * \param output The weston_output object to add
4687 * \param compositor The compositor instance.
4688 *
4689 * Also notifies the compositor that an output is pending for
4690 * configuration.
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004691 *
4692 * The opposite of this operation is built into weston_output_destroy().
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004693 *
4694 * \memberof weston_output
4695 * \internal
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004696 */
4697WL_EXPORT void
4698weston_compositor_add_pending_output(struct weston_output *output,
4699 struct weston_compositor *compositor)
4700{
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004701 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004702 wl_list_insert(compositor->pending_output_list.prev, &output->link);
4703 wl_signal_emit(&compositor->output_pending_signal, output);
4704}
4705
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004706/** Constructs a weston_output object that can be used by the compositor.
4707 *
4708 * \param output The weston_output object that needs to be enabled.
4709 *
4710 * Output coordinates are calculated and each new output is by default
4711 * assigned to the right of previous one.
4712 *
4713 * Sets up the transformation, zoom, and geometry of the output using
4714 * the properties that need to be configured by the compositor.
4715 *
4716 * Establishes a repaint timer for the output with the relevant display
4717 * object's event loop. See output_repaint_timer_handler().
4718 *
4719 * The output is assigned an ID. Weston can support up to 32 distinct
4720 * outputs, with IDs numbered from 0-31; the compositor's output_id_pool
4721 * is referred to and used to find the first available ID number, and
4722 * then this ID is marked as used in output_id_pool.
4723 *
4724 * The output is also assigned a Wayland global with the wl_output
4725 * external interface.
4726 *
4727 * Backend specific function is called to set up the output output.
4728 *
4729 * Output is added to the compositor's output list
4730 *
4731 * If the backend specific function fails, the weston_output object
4732 * is returned to a state it was before calling this function and
4733 * is added to the compositor's pending_output_list in case it needs
4734 * to be reconfigured or just so it can be destroyed at shutdown.
4735 *
4736 * 0 is returned on success, -1 on failure.
4737 */
4738WL_EXPORT int
4739weston_output_enable(struct weston_output *output)
4740{
Armin Krezović782f5df2016-09-30 14:11:11 +02004741 struct weston_compositor *c = output->compositor;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004742 struct weston_output *iterator;
4743 int x = 0, y = 0;
4744
4745 assert(output->enable);
4746
Armin Krezović782f5df2016-09-30 14:11:11 +02004747 iterator = container_of(c->output_list.prev,
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004748 struct weston_output, link);
4749
Armin Krezović782f5df2016-09-30 14:11:11 +02004750 if (!wl_list_empty(&c->output_list))
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004751 x = iterator->x + iterator->width;
4752
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004753 /* Make sure the scale is set up */
4754 assert(output->scale);
4755
4756 /* Make sure we have a transform set */
4757 assert(output->transform != UINT32_MAX);
4758
Armin Krezović782f5df2016-09-30 14:11:11 +02004759 /* Verify we haven't reached the limit of 32 available output IDs */
4760 assert(ffs(~c->output_id_pool) > 0);
4761
4762 output->x = x;
4763 output->y = y;
4764 output->dirty = 1;
4765 output->original_scale = output->scale;
4766
4767 weston_output_transform_scale_init(output, output->transform, output->scale);
4768 weston_output_init_zoom(output);
4769
4770 weston_output_init_geometry(output, x, y);
4771 weston_output_damage(output);
4772
4773 wl_signal_init(&output->frame_signal);
4774 wl_signal_init(&output->destroy_signal);
4775 wl_list_init(&output->animation_list);
4776 wl_list_init(&output->resource_list);
4777 wl_list_init(&output->feedback_list);
Armin Krezović782f5df2016-09-30 14:11:11 +02004778
Armin Krezović782f5df2016-09-30 14:11:11 +02004779 /* Invert the output id pool and look for the lowest numbered
4780 * switch (the least significant bit). Take that bit's position
4781 * as our ID, and mark it used in the compositor's output_id_pool.
4782 */
4783 output->id = ffs(~output->compositor->output_id_pool) - 1;
4784 output->compositor->output_id_pool |= 1u << output->id;
4785
4786 output->global =
4787 wl_global_create(c->wl_display, &wl_output_interface, 3,
4788 output, bind_output);
4789
4790 output->enabled = true;
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004791
4792 /* Enable the output (set up the crtc or create a
4793 * window representing the output, set up the
4794 * renderer, etc)
4795 */
4796 if (output->enable(output) < 0) {
4797 weston_log("Enabling output \"%s\" failed.\n", output->name);
4798
4799 weston_output_enable_undo(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004800 return -1;
4801 }
4802
4803 weston_compositor_add_output(output->compositor, output);
4804
4805 return 0;
4806}
4807
4808/** Converts a weston_output object to a pending output state, so it
4809 ** can be configured again or destroyed.
4810 *
4811 * \param output The weston_output object that needs to be disabled.
4812 *
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004813 * Calls a backend specific function to disable an output, in case
4814 * such function exists.
4815 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004816 * The backend specific disable function may choose to postpone the disabling
4817 * by returning a negative value, in which case this function returns early.
4818 * In that case the backend will guarantee the output will be disabled soon
4819 * by the backend calling this function again. One must not attempt to re-enable
4820 * the output until that happens.
4821 *
4822 * Otherwise, if the output is being used by the compositor, it is removed
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004823 * from weston's output_list (see weston_compositor_remove_output())
4824 * and is returned to a state it was before weston_output_enable()
4825 * was ran (see weston_output_enable_undo()).
4826 *
Pekka Paalanenc65df642017-03-29 15:45:46 +03004827 * See weston_output_init() for more information on the
4828 * state output is returned to.
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004829 */
4830WL_EXPORT void
4831weston_output_disable(struct weston_output *output)
4832{
4833 assert(output->disable);
4834
4835 /* Should we rename this? */
4836 output->destroying = 1;
4837
Pekka Paalanenc65df642017-03-29 15:45:46 +03004838 /* Disable is called unconditionally also for not-enabled outputs,
4839 * because at compositor start-up, if there is an output that is
4840 * already on but the compositor wants to turn it off, we have to
4841 * forward the turn-off to the backend so it knows to do it.
4842 * The backend cannot initially turn off everything, because it
4843 * would cause unnecessary mode-sets for all outputs the compositor
4844 * wants to be on.
4845 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004846 if (output->disable(output) < 0)
4847 return;
4848
4849 if (output->enabled) {
4850 weston_compositor_remove_output(output);
4851 weston_output_enable_undo(output);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004852 }
4853
4854 output->destroying = 0;
4855}
4856
4857/** Emits a signal to indicate that there are outputs waiting to be configured.
4858 *
4859 * \param compositor The compositor instance
4860 */
4861WL_EXPORT void
4862weston_pending_output_coldplug(struct weston_compositor *compositor)
4863{
4864 struct weston_output *output, *next;
4865
4866 wl_list_for_each_safe(output, next, &compositor->pending_output_list, link)
4867 wl_signal_emit(&compositor->output_pending_signal, output);
4868}
4869
Pekka Paalanenee16ea92017-03-29 16:53:50 +03004870/** Uninitialize an output
4871 *
4872 * Removes the output from the list of enabled outputs if necessary, but
4873 * does not call the backend's output disable function. The output will no
4874 * longer be in the list of pending outputs either.
4875 *
4876 * All fields of weston_output become uninitialized, i.e. should not be used
4877 * anymore. The caller can free the memory after this.
4878 *
4879 * \memberof weston_output
4880 * \internal
4881 */
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004882WL_EXPORT void
4883weston_output_destroy(struct weston_output *output)
4884{
4885 output->destroying = 1;
4886
4887 if (output->enabled) {
4888 weston_compositor_remove_output(output);
4889 weston_output_enable_undo(output);
4890 }
4891
Pekka Paalanenf0ca7962017-03-29 16:20:19 +03004892 wl_list_remove(&output->link);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02004893 free(output->name);
4894}
4895
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004896static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004897destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004898{
Jonny Lamb74130762013-11-26 18:19:46 +01004899 struct weston_surface *surface =
4900 wl_resource_get_user_data(resource);
4901
Pekka Paalanen4826f872016-04-22 14:14:38 +03004902 if (!surface)
4903 return;
4904
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004905 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004906 surface->pending.buffer_viewport.buffer.src_width =
4907 wl_fixed_from_int(-1);
4908 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004909 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004910}
4911
4912static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004913viewport_destroy(struct wl_client *client,
4914 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004915{
4916 wl_resource_destroy(resource);
4917}
4918
4919static void
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004920viewport_set_source(struct wl_client *client,
4921 struct wl_resource *resource,
4922 wl_fixed_t src_x,
4923 wl_fixed_t src_y,
4924 wl_fixed_t src_width,
4925 wl_fixed_t src_height)
4926{
4927 struct weston_surface *surface =
4928 wl_resource_get_user_data(resource);
4929
Pekka Paalanen4826f872016-04-22 14:14:38 +03004930 if (!surface) {
4931 wl_resource_post_error(resource,
4932 WP_VIEWPORT_ERROR_NO_SURFACE,
4933 "wl_surface for this viewport is no longer exists");
4934 return;
4935 }
4936
4937 assert(surface->viewport_resource == resource);
Pekka Paalanen201769a2016-04-26 14:42:11 +03004938 assert(surface->resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004939
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004940 if (src_width == wl_fixed_from_int(-1) &&
Pekka Paalanen201769a2016-04-26 14:42:11 +03004941 src_height == wl_fixed_from_int(-1) &&
4942 src_x == wl_fixed_from_int(-1) &&
4943 src_y == wl_fixed_from_int(-1)) {
4944 /* unset source rect */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004945 surface->pending.buffer_viewport.buffer.src_width =
4946 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004947 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004948 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004949 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004950
Pekka Paalanen201769a2016-04-26 14:42:11 +03004951 if (src_width <= 0 || src_height <= 0 || src_x < 0 || src_y < 0) {
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004952 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004953 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen201769a2016-04-26 14:42:11 +03004954 "wl_surface@%d viewport source "
4955 "w=%f <= 0, h=%f <= 0, x=%f < 0, or y=%f < 0",
4956 wl_resource_get_id(surface->resource),
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004957 wl_fixed_to_double(src_width),
Pekka Paalanen201769a2016-04-26 14:42:11 +03004958 wl_fixed_to_double(src_height),
4959 wl_fixed_to_double(src_x),
4960 wl_fixed_to_double(src_y));
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004961 return;
4962 }
4963
4964 surface->pending.buffer_viewport.buffer.src_x = src_x;
4965 surface->pending.buffer_viewport.buffer.src_y = src_y;
4966 surface->pending.buffer_viewport.buffer.src_width = src_width;
4967 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004968 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004969}
4970
4971static void
4972viewport_set_destination(struct wl_client *client,
4973 struct wl_resource *resource,
4974 int32_t dst_width,
4975 int32_t dst_height)
4976{
4977 struct weston_surface *surface =
4978 wl_resource_get_user_data(resource);
4979
Pekka Paalanen4826f872016-04-22 14:14:38 +03004980 if (!surface) {
4981 wl_resource_post_error(resource,
4982 WP_VIEWPORT_ERROR_NO_SURFACE,
4983 "wl_surface for this viewport no longer exists");
4984 return;
4985 }
4986
4987 assert(surface->viewport_resource == resource);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004988
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004989 if (dst_width == -1 && dst_height == -1) {
4990 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004991 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004992 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004993 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004994 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004995
4996 if (dst_width <= 0 || dst_height <= 0) {
4997 wl_resource_post_error(resource,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03004998 WP_VIEWPORT_ERROR_BAD_VALUE,
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004999 "destination size must be positive (%dx%d)",
5000 dst_width, dst_height);
5001 return;
5002 }
5003
5004 surface->pending.buffer_viewport.surface.width = dst_width;
5005 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02005006 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005007}
5008
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005009static const struct wp_viewport_interface viewport_interface = {
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005010 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005011 viewport_set_source,
5012 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01005013};
5014
5015static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005016viewporter_destroy(struct wl_client *client,
5017 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005018{
5019 wl_resource_destroy(resource);
5020}
5021
5022static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005023viewporter_get_viewport(struct wl_client *client,
5024 struct wl_resource *viewporter,
5025 uint32_t id,
5026 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005027{
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005028 int version = wl_resource_get_version(viewporter);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005029 struct weston_surface *surface =
5030 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005031 struct wl_resource *resource;
5032
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005033 if (surface->viewport_resource) {
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005034 wl_resource_post_error(viewporter,
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005035 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005036 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01005037 return;
5038 }
5039
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005040 resource = wl_resource_create(client, &wp_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02005041 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005042 if (resource == NULL) {
5043 wl_client_post_no_memory(client);
5044 return;
5045 }
5046
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005047 wl_resource_set_implementation(resource, &viewport_interface,
5048 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01005049
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02005050 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005051}
5052
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005053static const struct wp_viewporter_interface viewporter_interface = {
5054 viewporter_destroy,
5055 viewporter_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01005056};
5057
5058static void
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005059bind_viewporter(struct wl_client *client,
5060 void *data, uint32_t version, uint32_t id)
Jonny Lamb8ae35902013-11-26 18:19:45 +01005061{
5062 struct wl_resource *resource;
5063
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005064 resource = wl_resource_create(client, &wp_viewporter_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005065 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01005066 if (resource == NULL) {
5067 wl_client_post_no_memory(client);
5068 return;
5069 }
5070
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005071 wl_resource_set_implementation(resource, &viewporter_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01005072 NULL, NULL);
5073}
5074
5075static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04005076destroy_presentation_feedback(struct wl_resource *feedback_resource)
5077{
5078 struct weston_presentation_feedback *feedback;
5079
5080 feedback = wl_resource_get_user_data(feedback_resource);
5081
5082 wl_list_remove(&feedback->link);
5083 free(feedback);
5084}
5085
5086static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005087presentation_destroy(struct wl_client *client, struct wl_resource *resource)
5088{
5089 wl_resource_destroy(resource);
5090}
5091
5092static void
5093presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005094 struct wl_resource *presentation_resource,
5095 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005096 uint32_t callback)
5097{
Pekka Paalanen133e4392014-09-23 22:08:46 -04005098 struct weston_surface *surface;
5099 struct weston_presentation_feedback *feedback;
5100
5101 surface = wl_resource_get_user_data(surface_resource);
5102
Bryce Harringtonde16d892014-11-20 22:21:57 -08005103 feedback = zalloc(sizeof *feedback);
5104 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04005105 goto err_calloc;
5106
5107 feedback->resource = wl_resource_create(client,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005108 &wp_presentation_feedback_interface,
Pekka Paalanen133e4392014-09-23 22:08:46 -04005109 1, callback);
5110 if (!feedback->resource)
5111 goto err_create;
5112
5113 wl_resource_set_implementation(feedback->resource, NULL, feedback,
5114 destroy_presentation_feedback);
5115
5116 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
5117
5118 return;
5119
5120err_create:
5121 free(feedback);
5122
5123err_calloc:
5124 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005125}
5126
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005127static const struct wp_presentation_interface presentation_implementation = {
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005128 presentation_destroy,
5129 presentation_feedback
5130};
5131
5132static void
5133bind_presentation(struct wl_client *client,
5134 void *data, uint32_t version, uint32_t id)
5135{
5136 struct weston_compositor *compositor = data;
5137 struct wl_resource *resource;
5138
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005139 resource = wl_resource_create(client, &wp_presentation_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005140 version, id);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005141 if (resource == NULL) {
5142 wl_client_post_no_memory(client);
5143 return;
5144 }
5145
5146 wl_resource_set_implementation(resource, &presentation_implementation,
5147 compositor, NULL);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005148 wp_presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005149}
5150
5151static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05005152compositor_bind(struct wl_client *client,
5153 void *data, uint32_t version, uint32_t id)
5154{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005155 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05005156 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05005157
Jason Ekstranda85118c2013-06-27 20:17:02 -05005158 resource = wl_resource_create(client, &wl_compositor_interface,
Derek Foreman1909c102015-11-26 14:17:47 -06005159 version, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07005160 if (resource == NULL) {
5161 wl_client_post_no_memory(client);
5162 return;
5163 }
5164
5165 wl_resource_set_implementation(resource, &compositor_interface,
5166 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05005167}
5168
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005169WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005170weston_environment_get_fd(const char *env)
5171{
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005172 char *e;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005173 int fd, flags;
5174
5175 e = getenv(env);
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07005176 if (!e || !safe_strtoint(e, &fd))
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02005177 return -1;
5178
5179 flags = fcntl(fd, F_GETFD);
5180 if (flags == -1)
5181 return -1;
5182
5183 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
5184 unsetenv(env);
5185
5186 return fd;
5187}
5188
Pekka Paalanenb5026542014-11-12 15:09:24 +02005189static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05005190timeline_key_binding_handler(struct weston_keyboard *keyboard, uint32_t time,
Pekka Paalanenb5026542014-11-12 15:09:24 +02005191 uint32_t key, void *data)
5192{
5193 struct weston_compositor *compositor = data;
5194
5195 if (weston_timeline_enabled_)
5196 weston_timeline_close();
5197 else
5198 weston_timeline_open(compositor);
5199}
5200
Giulio Camuffo459137b2014-10-11 23:56:24 +03005201/** Create the compositor.
5202 *
5203 * This functions creates and initializes a compositor instance.
5204 *
5205 * \param display The Wayland display to be used.
5206 * \param user_data A pointer to an object that can later be retrieved
5207 * using the \ref weston_compositor_get_user_data function.
5208 * \return The compositor instance on success or NULL on failure.
5209 */
5210WL_EXPORT struct weston_compositor *
5211weston_compositor_create(struct wl_display *display, void *user_data)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04005212{
Giulio Camuffo459137b2014-10-11 23:56:24 +03005213 struct weston_compositor *ec;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05005214 struct wl_event_loop *loop;
Ossama Othmana50e6e42013-05-14 09:48:26 -07005215
Giulio Camuffo459137b2014-10-11 23:56:24 +03005216 ec = zalloc(sizeof *ec);
5217 if (!ec)
5218 return NULL;
5219
5220 ec->wl_display = display;
5221 ec->user_data = user_data;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005222 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07005223 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005224 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03005225 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03005226 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005227 wl_signal_init(&ec->idle_signal);
5228 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02005229 wl_signal_init(&ec->show_input_panel_signal);
5230 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02005231 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01005232 wl_signal_init(&ec->seat_created_signal);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005233 wl_signal_init(&ec->output_pending_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01005234 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02005235 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005236 wl_signal_init(&ec->output_moved_signal);
David Fort0de859e2016-05-27 23:22:57 +02005237 wl_signal_init(&ec->output_resized_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07005238 wl_signal_init(&ec->session_signal);
5239 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005240
Casey Dahlin58ba1372012-04-19 22:50:08 -04005241 ec->output_id_pool = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03005242 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
Casey Dahlin58ba1372012-04-19 22:50:08 -04005243
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02005244 ec->activate_serial = 1;
5245
Derek Foreman152254b2015-11-26 14:17:48 -06005246 if (!wl_global_create(ec->wl_display, &wl_compositor_interface, 4,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005247 ec, compositor_bind))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005248 goto fail;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05005249
Giulio Camuffo954f1832014-10-11 18:27:30 +03005250 if (!wl_global_create(ec->wl_display, &wl_subcompositor_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005251 ec, bind_subcompositor))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005252 goto fail;
Pekka Paalanene67858b2013-04-25 13:57:42 +03005253
Pekka Paalanene95ad5c2016-04-15 14:47:08 +03005254 if (!wl_global_create(ec->wl_display, &wp_viewporter_interface, 1,
Pekka Paalanen9c5a0d92016-04-15 16:42:49 +03005255 ec, bind_viewporter))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005256 goto fail;
Jonny Lamb8ae35902013-11-26 18:19:45 +01005257
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02005258 if (!wl_global_create(ec->wl_display, &wp_presentation_interface, 1,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005259 ec, bind_presentation))
Giulio Camuffo459137b2014-10-11 23:56:24 +03005260 goto fail;
Pekka Paalanen31f7d782014-09-23 22:08:43 -04005261
Jonas Ådahl30d61d82014-10-22 21:21:17 +02005262 if (weston_input_init(ec) != 0)
5263 goto fail;
5264
Jason Ekstranda7af7042013-10-12 22:38:11 -05005265 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005266 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005267 wl_list_init(&ec->layer_list);
5268 wl_list_init(&ec->seat_list);
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005269 wl_list_init(&ec->pending_output_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005270 wl_list_init(&ec->output_list);
5271 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01005272 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005273 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005274 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005275 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005276 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01005277
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005278 wl_list_init(&ec->plugin_api_list);
5279
Xiong Zhang97116532013-10-23 13:58:31 +08005280 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02005281 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005282
Giulio Camuffo459137b2014-10-11 23:56:24 +03005283 wl_data_device_manager_init(ec->wl_display);
5284
5285 wl_display_init_shm(ec->wl_display);
5286
5287 loop = wl_display_get_event_loop(ec->wl_display);
5288 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Daniel Stone6847b852017-03-01 11:34:08 +00005289 ec->repaint_timer =
5290 wl_event_loop_add_timer(loop, output_repaint_timer_handler,
5291 ec);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005292
Quentin Glidic82681572016-12-17 13:40:51 +01005293 weston_layer_init(&ec->fade_layer, ec);
5294 weston_layer_init(&ec->cursor_layer, ec);
5295
5296 weston_layer_set_position(&ec->fade_layer, WESTON_LAYER_POSITION_FADE);
5297 weston_layer_set_position(&ec->cursor_layer,
5298 WESTON_LAYER_POSITION_CURSOR);
Giulio Camuffo459137b2014-10-11 23:56:24 +03005299
5300 weston_compositor_add_debug_binding(ec, KEY_T,
5301 timeline_key_binding_handler, ec);
5302
Giulio Camuffo459137b2014-10-11 23:56:24 +03005303 return ec;
5304
5305fail:
5306 free(ec);
5307 return NULL;
5308}
5309
Benjamin Franzkeb8263022011-08-30 11:32:47 +02005310WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005311weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07005312{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005313 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07005314
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005315 wl_event_source_remove(ec->idle_source);
5316
Matt Roper361d2ad2011-08-29 13:52:23 -07005317 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02005318 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07005319 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02005320
Armin Krezovića01ab6d2016-09-30 14:11:02 +02005321 /* Destroy all pending outputs associated with this compositor */
5322 wl_list_for_each_safe(output, next, &ec->pending_output_list, link)
5323 output->destroy(output);
5324
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02005325 if (ec->renderer)
5326 ec->renderer->destroy(ec);
5327
Daniel Stone325fc2d2012-05-30 16:31:58 +01005328 weston_binding_list_destroy_all(&ec->key_binding_list);
Ryo Munakata27135af2015-07-17 13:07:42 +09005329 weston_binding_list_destroy_all(&ec->modifier_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005330 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01005331 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005332 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005333 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02005334
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04005335 weston_plane_release(&ec->primary_plane);
Matt Roper361d2ad2011-08-29 13:52:23 -07005336}
5337
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005338WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04005339weston_compositor_exit_with_code(struct weston_compositor *compositor,
5340 int exit_code)
5341{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02005342 if (compositor->exit_code == EXIT_SUCCESS)
5343 compositor->exit_code = exit_code;
5344
Giulio Camuffo459137b2014-10-11 23:56:24 +03005345 weston_compositor_exit(compositor);
Frederic Plourdec336f062014-10-29 14:44:33 -04005346}
5347
5348WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005349weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
5350 const struct weston_pointer_grab_interface *interface)
5351{
5352 struct weston_seat *seat;
5353
5354 ec->default_pointer_grab = interface;
5355 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05005356 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5357
5358 if (pointer)
5359 weston_pointer_set_default_grab(pointer, interface);
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005360 }
5361}
5362
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04005363WL_EXPORT int
5364weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
5365 clockid_t clk_id)
5366{
5367 struct timespec ts;
5368
5369 if (clock_gettime(clk_id, &ts) < 0)
5370 return -1;
5371
5372 compositor->presentation_clock = clk_id;
5373
5374 return 0;
5375}
5376
5377/*
5378 * For choosing the software clock, when the display hardware or API
5379 * does not expose a compatible presentation timestamp.
5380 */
5381WL_EXPORT int
5382weston_compositor_set_presentation_clock_software(
5383 struct weston_compositor *compositor)
5384{
5385 /* In order of preference */
5386 static const clockid_t clocks[] = {
5387 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
5388 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
5389 CLOCK_MONOTONIC, /* no jumps, may crawl */
5390 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
5391 CLOCK_REALTIME /* may jump and crawl */
5392 };
5393 unsigned i;
5394
5395 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
5396 if (weston_compositor_set_presentation_clock(compositor,
5397 clocks[i]) == 0)
5398 return 0;
5399
5400 weston_log("Error: no suitable presentation clock available.\n");
5401
5402 return -1;
5403}
5404
Pekka Paalanen662f3842015-03-18 12:17:26 +02005405/** Read the current time from the Presentation clock
5406 *
5407 * \param compositor
5408 * \param ts[out] The current time.
5409 *
5410 * \note Reading the current time in user space is always imprecise to some
5411 * degree.
5412 *
5413 * This function is never meant to fail. If reading the clock does fail,
5414 * an error message is logged and a zero time is returned. Callers are not
5415 * supposed to detect or react to failures.
5416 */
5417WL_EXPORT void
5418weston_compositor_read_presentation_clock(
5419 const struct weston_compositor *compositor,
5420 struct timespec *ts)
5421{
5422 static bool warned;
5423 int ret;
5424
5425 ret = clock_gettime(compositor->presentation_clock, ts);
5426 if (ret < 0) {
5427 ts->tv_sec = 0;
5428 ts->tv_nsec = 0;
5429
5430 if (!warned)
5431 weston_log("Error: failure to read "
5432 "the presentation clock %#x: '%m' (%d)\n",
5433 compositor->presentation_clock, errno);
5434 warned = true;
5435 }
5436}
5437
Pekka Paalanen230f3b12014-09-29 14:18:40 -04005438/** Import dmabuf buffer into current renderer
5439 *
5440 * \param compositor
5441 * \param buffer the dmabuf buffer to import
5442 * \return true on usable buffers, false otherwise
5443 *
5444 * This function tests that the linux_dmabuf_buffer is usable
5445 * for the current renderer. Returns false on unusable buffers. Usually
5446 * usability is tested by importing the dmabufs for composition.
5447 *
5448 * This hook is also used for detecting if the renderer supports
5449 * dmabufs at all. If the renderer hook is NULL, dmabufs are not
5450 * supported.
5451 * */
5452WL_EXPORT bool
5453weston_compositor_import_dmabuf(struct weston_compositor *compositor,
5454 struct linux_dmabuf_buffer *buffer)
5455{
5456 struct weston_renderer *renderer;
5457
5458 renderer = compositor->renderer;
5459
5460 if (renderer->import_dmabuf == NULL)
5461 return false;
5462
5463 return renderer->import_dmabuf(compositor, buffer);
5464}
5465
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005466WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05005467weston_version(int *major, int *minor, int *micro)
5468{
5469 *major = WESTON_VERSION_MAJOR;
5470 *minor = WESTON_VERSION_MINOR;
5471 *micro = WESTON_VERSION_MICRO;
5472}
5473
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005474WL_EXPORT void *
5475weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005476{
Derek Foreman3f86e502015-06-08 11:46:54 -05005477 const char *builddir = getenv("WESTON_BUILD_DIR");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005478 char path[PATH_MAX];
5479 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +00005480 size_t len;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005481
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005482 if (name == NULL)
5483 return NULL;
5484
Derek Foreman3f86e502015-06-08 11:46:54 -05005485 if (name[0] != '/') {
5486 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +00005487 len = snprintf(path, sizeof path, "%s/.libs/%s",
5488 builddir, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005489 else
Daniel Stonebeb97e52016-11-28 12:13:54 +00005490 len = snprintf(path, sizeof path, "%s/%s",
5491 LIBWESTON_MODULEDIR, name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005492 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +00005493 len = snprintf(path, sizeof path, "%s", name);
Derek Foreman3f86e502015-06-08 11:46:54 -05005494 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005495
Daniel Stonebeb97e52016-11-28 12:13:54 +00005496 /* snprintf returns the length of the string it would've written,
5497 * _excluding_ the NUL byte. So even being equal to the size of
5498 * our buffer is an error here. */
5499 if (len >= sizeof path)
5500 return NULL;
5501
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005502 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
5503 if (module) {
5504 weston_log("Module '%s' already loaded\n", path);
5505 dlclose(module);
5506 return NULL;
5507 }
5508
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005509 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04005510 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005511 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005512 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005513 return NULL;
5514 }
5515
5516 init = dlsym(module, entrypoint);
5517 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03005518 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00005519 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005520 return NULL;
5521 }
5522
5523 return init;
5524}
5525
Giulio Camuffo459137b2014-10-11 23:56:24 +03005526
5527/** Destroys the compositor.
5528 *
5529 * This function cleans up the compositor state and destroys it.
5530 *
5531 * \param compositor The compositor to be destroyed.
5532 */
5533WL_EXPORT void
5534weston_compositor_destroy(struct weston_compositor *compositor)
5535{
5536 /* prevent further rendering while shutting down */
5537 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
5538
5539 wl_signal_emit(&compositor->destroy_signal, compositor);
5540
5541 weston_compositor_xkb_destroy(compositor);
5542
Giulio Camuffo2d24e642015-10-03 16:25:15 +03005543 if (compositor->backend)
5544 compositor->backend->destroy(compositor);
Pekka Paalanen827b5d22016-06-29 11:54:26 +02005545
5546 weston_plugin_api_destroy_list(compositor);
5547
Giulio Camuffo459137b2014-10-11 23:56:24 +03005548 free(compositor);
5549}
5550
5551/** Instruct the compositor to exit.
5552 *
5553 * This functions does not directly destroy the compositor object, it merely
5554 * command it to start the tear down process. It is not guaranteed that the
5555 * tear down will happen immediately.
5556 *
5557 * \param compositor The compositor to tear down.
5558 */
5559WL_EXPORT void
5560weston_compositor_exit(struct weston_compositor *compositor)
5561{
5562 compositor->exit(compositor);
5563}
5564
5565/** Return the user data stored in the compositor.
5566 *
5567 * This function returns the user data pointer set with user_data parameter
5568 * to the \ref weston_compositor_create function.
5569 */
5570WL_EXPORT void *
5571weston_compositor_get_user_data(struct weston_compositor *compositor)
5572{
5573 return compositor->user_data;
5574}
Pekka Paalanendd186732016-06-03 14:49:54 +03005575
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005576static const char * const backend_map[] = {
5577 [WESTON_BACKEND_DRM] = "drm-backend.so",
5578 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
5579 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
5580 [WESTON_BACKEND_RDP] = "rdp-backend.so",
5581 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
5582 [WESTON_BACKEND_X11] = "x11-backend.so",
5583};
5584
Pekka Paalanendd186732016-06-03 14:49:54 +03005585/** Load a backend into a weston_compositor
5586 *
5587 * A backend must be loaded to make a weston_compositor work. A backend
5588 * provides input and output capabilities, and determines the renderer to use.
5589 *
5590 * \param compositor A compositor that has not had a backend loaded yet.
5591 * \param backend Name of the backend file.
5592 * \param config_base A pointer to a backend-specific configuration
5593 * structure's 'base' member.
5594 *
5595 * \return 0 on success, or -1 on error.
5596 */
5597WL_EXPORT int
5598weston_compositor_load_backend(struct weston_compositor *compositor,
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005599 enum weston_compositor_backend backend,
Pekka Paalanendd186732016-06-03 14:49:54 +03005600 struct weston_backend_config *config_base)
5601{
5602 int (*backend_init)(struct weston_compositor *c,
Pekka Paalanendd186732016-06-03 14:49:54 +03005603 struct weston_backend_config *config_base);
5604
Quentin Glidic887c0182016-07-10 11:00:53 +02005605 if (backend >= ARRAY_LENGTH(backend_map))
Pekka Paalanen50dbf382016-06-03 15:23:46 +03005606 return -1;
5607
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01005608 backend_init = weston_load_module(backend_map[backend], "weston_backend_init");
Pekka Paalanendd186732016-06-03 14:49:54 +03005609 if (!backend_init)
5610 return -1;
5611
Pekka Paalanena256c5e2016-06-03 14:56:18 +03005612 return backend_init(compositor, config_base);
Pekka Paalanendd186732016-06-03 14:49:54 +03005613}
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005614
5615WL_EXPORT int
5616weston_compositor_load_xwayland(struct weston_compositor *compositor)
5617{
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005618 int (*module_init)(struct weston_compositor *ec);
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005619
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005620 module_init = weston_load_module("xwayland.so", "weston_module_init");
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005621 if (!module_init)
5622 return -1;
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +01005623 if (module_init(compositor) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +02005624 return -1;
5625 return 0;
5626}