hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
From d7f5695dac0a1fbf8efbab65f40ff0b83f16e922 Mon Sep 17 00:00:00 2001
From: Jeffy Chen <jeffy.chen@rock-chips.com>
Date: Thu, 19 Nov 2020 09:41:47 +0800
Subject: [PATCH 15/74] backend-drm: Support mirror mode
 
Set env "WESTON_DRM_MIRROR" to enable mirror mode, and set env
"WESTON_DRM_KEEP_RATIO" to keep the aspect ratio.
 
Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
---
 clients/desktop-shell.c               |   9 +-
 desktop-shell/shell.c                 |   3 +
 include/libweston/libweston.h         |   4 +
 libweston/backend-drm/drm-gbm.c       |   4 +-
 libweston/backend-drm/drm-internal.h  |  10 +
 libweston/backend-drm/drm.c           | 328 +++++++++++++++++++++++++-
 libweston/backend-drm/meson.build     |   3 +-
 libweston/backend-drm/state-propose.c |  22 +-
 libweston/compositor.c                |  24 +-
 libweston/input.c                     |   7 +
 meson.build                           |   5 +
 11 files changed, 398 insertions(+), 21 deletions(-)
 
diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c
index 56c3976..8711399 100644
--- a/clients/desktop-shell.c
+++ b/clients/desktop-shell.c
@@ -1062,9 +1062,14 @@ desktop_shell_configure(void *data,
             struct wl_surface *surface,
             int32_t width, int32_t height)
 {
-    struct window *window = wl_surface_get_user_data(surface);
-    struct surface *s = window_get_user_data(window);
+    struct window *window;
+    struct surface *s;
+
+    if (!surface)
+        return;
 
+    window = wl_surface_get_user_data(surface);
+    s = window_get_user_data(window);
     s->configure(data, desktop_shell, edges, window, width, height);
 }
 
diff --git a/desktop-shell/shell.c b/desktop-shell/shell.c
index 515c57f..18bec12 100644
--- a/desktop-shell/shell.c
+++ b/desktop-shell/shell.c
@@ -4280,6 +4280,9 @@ weston_view_set_initial_position(struct weston_view *view,
     }
 
     wl_list_for_each(output, &compositor->output_list, link) {
+        if (output->unavailable)
+            continue;
+
         if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
             target_output = output;
             break;
diff --git a/include/libweston/libweston.h b/include/libweston/libweston.h
index bdecc19..8868622 100644
--- a/include/libweston/libweston.h
+++ b/include/libweston/libweston.h
@@ -411,7 +411,11 @@ struct weston_output {
      */
     void (*detach_head)(struct weston_output *output,
                 struct weston_head *head);
+
+    bool unavailable;
 };
+#define weston_output_valid(o) \
+    ((o) && !(o)->destroying && !(o)->unavailable)
 
 enum weston_pointer_motion_mask {
     WESTON_POINTER_MOTION_ABS = 1 << 0,
diff --git a/libweston/backend-drm/drm-gbm.c b/libweston/backend-drm/drm-gbm.c
index d0a4c6c..d7bd05f 100644
--- a/libweston/backend-drm/drm-gbm.c
+++ b/libweston/backend-drm/drm-gbm.c
@@ -265,8 +265,8 @@ drm_output_fini_egl(struct drm_output *output)
     /* Destroying the GBM surface will destroy all our GBM buffers,
      * regardless of refcount. Ensure we destroy them here. */
     if (!b->shutting_down &&
-        output->scanout_plane->state_cur->fb &&
-        output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) {
+        output->scanout_plane->state_cur->fb && (output->is_mirror ||
+        output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE)) {
         drm_plane_reset_state(output->scanout_plane);
     }
 
diff --git a/libweston/backend-drm/drm-internal.h b/libweston/backend-drm/drm-internal.h
index 996f587..1625be2 100644
--- a/libweston/backend-drm/drm-internal.h
+++ b/libweston/backend-drm/drm-internal.h
@@ -346,6 +346,8 @@ struct drm_backend {
 
     int virtual_width;
     int virtual_height;
+
+    bool mirror_mode;
 };
 
 struct drm_mode {
@@ -603,6 +605,10 @@ struct drm_output {
     int current_image;
     pixman_region32_t previous_damage;
 
+    /* Wrap fb for scale/rotate usage */
+    struct drm_fb *wrap[2];
+    int next_wrap;
+
     struct vaapi_recorder *recorder;
     struct wl_listener recorder_frame_listener;
 
@@ -616,6 +622,10 @@ struct drm_output {
 
     /* The dummy framebuffer for SET_CRTC. */
     struct drm_fb *fb_dummy;
+
+    bool is_mirror;
+
+    pixman_box32_t plane_bounds;
 };
 
 static inline struct drm_head *
diff --git a/libweston/backend-drm/drm.c b/libweston/backend-drm/drm.c
index 1c839c5..696df68 100644
--- a/libweston/backend-drm/drm.c
+++ b/libweston/backend-drm/drm.c
@@ -67,6 +67,11 @@
 #include "linux-dmabuf-unstable-v1-server-protocol.h"
 #include "linux-explicit-synchronization.h"
 
+#ifdef HAVE_RGA
+#include <rga/rga.h>
+#include <rga/RgaApi.h>
+#endif
+
 static const char default_seat[] = "seat0";
 
 static inline bool
@@ -85,6 +90,118 @@ drm_head_is_external(struct drm_head *head)
     }
 };
 
+static int
+drm_output_get_rotation(struct drm_output *output)
+{
+    switch (output->base.transform) {
+    case WL_OUTPUT_TRANSFORM_90:
+    case WL_OUTPUT_TRANSFORM_FLIPPED_90:
+        return 90;
+    case WL_OUTPUT_TRANSFORM_180:
+    case WL_OUTPUT_TRANSFORM_FLIPPED_180:
+        return 180;
+    case WL_OUTPUT_TRANSFORM_270:
+    case WL_OUTPUT_TRANSFORM_FLIPPED_270:
+        return 270;
+    default:
+        return 0;
+    }
+}
+
+#ifdef HAVE_RGA
+static inline RgaSURF_FORMAT
+rga_get_format(const struct pixel_format_info *format)
+{
+    switch (format->bpp) {
+    case 32:
+        return RK_FORMAT_BGRX_8888;
+    case 16:
+        return RK_FORMAT_RGB_565;
+    default:
+        return RK_FORMAT_UNKNOWN;
+    }
+}
+#endif
+
+static int
+drm_copy_fb(struct drm_fb *src, struct drm_fb *dst, int rotation,
+        int src_width, int src_height)
+{
+#ifndef HAVE_RGA
+    /* TODO: Use pixman to do the copy */
+    weston_log("rga not supported\n");
+    return -1;
+#else
+    RgaSURF_FORMAT src_format, dst_format;
+    rga_info_t src_info = {0};
+    rga_info_t dst_info = {0};
+    int src_fd, dst_fd;
+    int ret;
+
+    static bool rga_supported = true;
+    static bool rga_inited = false;
+
+    if (!rga_supported)
+        return -1;
+
+    if (!rga_inited) {
+        ret = c_RkRgaInit();
+        if (ret < 0) {
+            weston_log("rga not supported\n");
+            rga_supported = false;
+            return ret;
+        }
+        rga_inited = true;
+    }
+
+    src_format = rga_get_format(src->format);
+    dst_format = rga_get_format(dst->format);
+
+    if (src_format == RK_FORMAT_UNKNOWN ||
+        dst_format == RK_FORMAT_UNKNOWN) {
+        weston_log("unsupported fb format\n");
+        return -1;
+    }
+
+    ret = drmPrimeHandleToFD(src->fd, src->handles[0],
+                 DRM_CLOEXEC, &src_fd);
+    if (ret < 0)
+        return ret;
+
+    ret = drmPrimeHandleToFD(dst->fd, dst->handles[0],
+                 DRM_CLOEXEC, &dst_fd);
+    if (ret < 0)
+        goto close_src;
+
+    src_info.fd = src_fd;
+    src_info.mmuFlag = 1;
+
+    rga_set_rect(&src_info.rect, 0, 0, src_width, src_height,
+             src->strides[0] * 8 / src->format->bpp, src->height,
+             src_format);
+
+    if (rotation == 90)
+        src_info.rotation = HAL_TRANSFORM_ROT_90;
+    else if (rotation == 180)
+        src_info.rotation = HAL_TRANSFORM_ROT_180;
+    else if (rotation == 270)
+        src_info.rotation = HAL_TRANSFORM_ROT_270;
+
+    dst_info.fd = dst_fd;
+    dst_info.mmuFlag = 1;
+
+    rga_set_rect(&dst_info.rect, 0, 0, dst->width, dst->height,
+             dst->strides[0] * 8 / dst->format->bpp, dst->height,
+             dst_format);
+
+    ret = c_RkRgaBlit(&src_info, &dst_info, NULL);
+    close(dst_fd);
+close_src:
+    close(src_fd);
+    return ret;
+#endif
+}
+
 static void
 drm_backend_update_outputs(struct drm_backend *b)
 {
@@ -94,6 +211,26 @@ drm_backend_update_outputs(struct drm_backend *b)
         return;
 
     primary = b->primary_head->base.output;
+
+    if (b->mirror_mode) {
+        wl_list_for_each(base, &b->compositor->output_list, link) {
+            struct drm_output *output = to_drm_output(base);
+            bool is_mirror = base != primary;
+
+            if (output->is_mirror == is_mirror)
+                continue;
+
+            /* Make mirrors unavailable for normal views */
+            output->base.unavailable = is_mirror;
+
+            output->is_mirror = is_mirror;
+            output->state_invalid = true;
+
+            weston_log("Output %s changed to %s output\n",
+                   base->name, is_mirror ? "mirror" : "main");
+        }
+    }
+
     if (!primary)
         return;
 
@@ -392,6 +529,69 @@ drm_output_render_pixman(struct drm_output_state *state,
     return drm_fb_ref(output->dumb[output->current_image]);
 }
 
+static struct drm_fb *
+drm_output_get_fb(struct drm_pending_state *pending_state,
+          struct weston_output *output_base)
+{
+    struct drm_output *output = to_drm_output(output_base);
+    struct drm_plane_state *scanout_state;
+    struct drm_output_state *state;
+    struct drm_fb *fb = output->scanout_plane->state_cur->fb;
+
+    state = drm_pending_state_get_output(pending_state, output);
+    if (!state)
+        return fb;
+
+    scanout_state =
+        drm_output_state_get_existing_plane(state,
+                            output->scanout_plane);
+    if (!scanout_state || !scanout_state->fb)
+        return fb;
+
+    return scanout_state->fb;
+}
+
+static void
+drm_output_try_destroy_wrap_fb(struct drm_output *output)
+{
+    if (output->wrap[0]) {
+        drm_fb_unref(output->wrap[0]);
+        output->wrap[0] = NULL;
+    }
+
+    if (output->wrap[1]) {
+        drm_fb_unref(output->wrap[1]);
+        output->wrap[1] = NULL;
+    }
+
+    output->next_wrap = 0;
+}
+
+static struct drm_fb *
+drm_output_get_wrap_fb(struct drm_backend *b, struct drm_output *output,
+               int width, int height)
+{
+    struct drm_fb *fb = output->wrap[output->next_wrap];
+
+    if (fb) {
+        if (fb->width == width && fb->height == height)
+            goto out;
+
+        drm_fb_unref(fb);
+    }
+
+    fb = drm_fb_create_dumb(b, width, height, output->gbm_format);
+    if (!fb) {
+        weston_log("failed to create wrap fb\n");
+        return NULL;
+    }
+
+    output->wrap[output->next_wrap] = fb;
+out:
+    output->next_wrap ^= 1;
+    return drm_fb_ref(fb);
+}
+
 void
 drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
 {
@@ -403,10 +603,13 @@ drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
         &scanout_plane->props[WDRM_PLANE_FB_DAMAGE_CLIPS];
     struct drm_backend *b = to_drm_backend(c);
     struct drm_mode *mode;
-    struct drm_fb *fb;
+    struct drm_fb *fb = NULL;
     pixman_region32_t scanout_damage;
     pixman_box32_t *rects;
     int n_rects;
+    int sw, sh, dx, dy, dw, dh;
+    int rotation = 0;
+    bool scaling;
 
     /* If we already have a client buffer promoted to scanout, then we don't
      * want to render. */
@@ -414,6 +617,35 @@ drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
     if (scanout_state->fb)
         return;
 
+    if (!output->is_mirror) {
+        struct drm_output *tmp;
+
+        /* Repaint all mirrors when updating main output */
+        wl_list_for_each(tmp, &b->compositor->output_list, base.link)
+            if (tmp->is_mirror)
+                weston_output_schedule_repaint(&tmp->base);
+    } else {
+        if (!b->primary_head)
+            goto out;
+
+        rotation = drm_output_get_rotation(output);
+
+        fb = drm_output_get_fb(state->pending_state,
+                       b->primary_head->base.output);
+        if (fb) {
+            drm_fb_ref(fb);
+
+            pixman_region32_init(&scanout_damage);
+            wl_signal_emit(&output->base.frame_signal,
+                       &scanout_damage);
+            pixman_region32_fini(&scanout_damage);
+        } else {
+            weston_compositor_damage_all(b->compositor);
+        }
+
+        goto out;
+    }
+
     /*
      * If we don't have any damage on the primary plane, and we already
      * have a renderer buffer active, we can reuse it; else we pass
@@ -433,24 +665,86 @@ drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
         fb = drm_output_render_gl(state, damage);
     }
 
+out:
     if (!fb) {
         drm_plane_state_put_back(scanout_state);
         return;
     }
 
+    sw = fb->width;
+    sh = fb->height;
+
+    dx = output->plane_bounds.x1;
+    dy = output->plane_bounds.y1;
+    dw = output->plane_bounds.x2 - output->plane_bounds.x1;
+    dh = output->plane_bounds.y2 - output->plane_bounds.y1;
+
+    if (!dw || !dh) {
+        mode = to_drm_mode(output->base.current_mode);
+        dw = mode->mode_info.hdisplay;
+        dh = mode->mode_info.vdisplay;
+    }
+
+    if (output->is_mirror && getenv("WESTON_DRM_KEEP_RATIO")) {
+        float src_ratio = (float) sw / sh;
+        float dst_ratio = (float) dw / dh;
+        int offset;
+
+        if (rotation % 180)
+            src_ratio = 1 / src_ratio;
+
+        if (src_ratio > dst_ratio) {
+            offset = dh - dw / src_ratio;
+            dy = offset / 2;
+            dh -= offset;
+        } else {
+            offset = dw - dh * src_ratio;
+            dx = offset / 2;
+            dw -= offset;
+        }
+    }
+
+    scaling = sw != dw || sh != dh;
+
+    if (rotation || (scaling && !output->scanout_plane->can_scale)) {
+        struct drm_fb *wrap_fb =
+            drm_output_get_wrap_fb(b, output, dw, dh);
+        if (!wrap_fb) {
+            weston_log("failed to get wrap fb\n");
+            goto err;
+        }
+
+        if (drm_copy_fb(fb, wrap_fb, rotation, sw, sh) < 0) {
+            weston_log("failed to copy fb\n");
+            goto err;
+        }
+
+        sw = dw;
+        sh = dh;
+
+        drm_fb_unref(fb);
+        fb = wrap_fb;
+    } else {
+        drm_output_try_destroy_wrap_fb(output);
+    }
+
     scanout_state->fb = fb;
+    fb = NULL;
+
     scanout_state->output = output;
 
     scanout_state->src_x = 0;
     scanout_state->src_y = 0;
-    scanout_state->src_w = fb->width << 16;
-    scanout_state->src_h = fb->height << 16;
+    scanout_state->src_w = sw << 16;
+    scanout_state->src_h = sh << 16;
 
-    mode = to_drm_mode(output->base.current_mode);
-    scanout_state->dest_x = 0;
-    scanout_state->dest_y = 0;
-    scanout_state->dest_w = mode->mode_info.hdisplay;
-    scanout_state->dest_h = mode->mode_info.vdisplay;
+    scanout_state->dest_x = dx;
+    scanout_state->dest_y = dy;
+    scanout_state->dest_w = dw;
+    scanout_state->dest_h = dh;
+
+    if (output->is_mirror)
+        return;
 
     pixman_region32_subtract(&c->primary_plane.damage,
                  &c->primary_plane.damage, damage);
@@ -499,6 +793,12 @@ drm_output_render(struct drm_output_state *state, pixman_region32_t *damage)
                   &scanout_state->damage_blob_id);
 
     pixman_region32_fini(&scanout_damage);
+    return;
+err:
+    if (fb)
+        drm_fb_unref(fb);
+
+    drm_plane_state_put_back(scanout_state);
 }
 
 static int
@@ -1296,8 +1596,8 @@ drm_output_fini_pixman(struct drm_output *output)
     /* Destroying the Pixman surface will destroy all our buffers,
      * regardless of refcount. Ensure we destroy them here. */
     if (!b->shutting_down &&
-        output->scanout_plane->state_cur->fb &&
-        output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB) {
+        output->scanout_plane->state_cur->fb && (output->is_mirror ||
+        output->scanout_plane->state_cur->fb->type == BUFFER_PIXMAN_DUMB)) {
         drm_plane_reset_state(output->scanout_plane);
     }
 
@@ -1975,6 +2275,8 @@ drm_output_destroy(struct weston_output *base)
     assert(!output->state_last);
     drm_output_state_free(output->state_cur);
 
+    drm_output_try_destroy_wrap_fb(output);
+
     free(output);
 }
 
@@ -3320,6 +3622,12 @@ drm_backend_create(struct weston_compositor *compositor,
     else
         b->resize_freeze_ms = DRM_RESIZE_FREEZE_MS;
 
+    buf = getenv("WESTON_DRM_MIRROR");
+    if (buf && buf[0] == '1') {
+        b->mirror_mode = true;
+        weston_log("Entering mirror mode.\n");
+    }
+
     b->state_invalid = true;
     b->drm.fd = -1;
 
diff --git a/libweston/backend-drm/meson.build b/libweston/backend-drm/meson.build
index 23db912..6dbd05d 100644
--- a/libweston/backend-drm/meson.build
+++ b/libweston/backend-drm/meson.build
@@ -38,7 +38,8 @@ deps_drm = [
     dep_libdrm,
     dep_libinput_backend,
     dependency('libudev', version: '>= 136'),
-    dep_backlight
+    dep_backlight,
+    dep_rga
 ]
 
 if get_option('renderer-gl')
diff --git a/libweston/backend-drm/state-propose.c b/libweston/backend-drm/state-propose.c
index 7b350aa..48e6de2 100644
--- a/libweston/backend-drm/state-propose.c
+++ b/libweston/backend-drm/state-propose.c
@@ -54,6 +54,21 @@ static const char *const drm_output_propose_state_mode_as_string[] = {
     [DRM_OUTPUT_PROPOSE_STATE_PLANES_ONLY]    = "plane-only state"
 };
 
+static bool
+drm_is_mirroring(struct drm_backend *b)
+{
+    struct drm_output *tmp;
+
+    if (!b->mirror_mode)
+        return false;
+
+    wl_list_for_each(tmp, &b->compositor->output_list, base.link)
+        if (tmp->is_mirror)
+            return true;
+
+    return false;
+}
+
 static const char *
 drm_propose_state_mode_to_string(enum drm_output_propose_state_mode mode)
 {
@@ -466,7 +481,7 @@ drm_output_try_view_on_plane(struct drm_plane *plane,
 
     switch (plane->type) {
     case WDRM_PLANE_TYPE_CURSOR:
-        if (b->cursors_are_broken) {
+        if (b->cursors_are_broken || drm_is_mirroring(b)) {
             availability = NO_PLANES_ACCEPTED;
             goto out;
         }
@@ -1102,7 +1117,10 @@ drm_assign_planes(struct weston_output *output_base, void *repaint_data)
     drm_debug(b, "\t[repaint] preparing state for output %s (%lu)\n",
           output_base->name, (unsigned long) output_base->id);
 
-    if (!b->sprites_are_broken && !output->virtual) {
+    /* Force single plane in mirror mode */
+    if (drm_is_mirroring(b)) {
+        drm_debug(b, "\t[state] no overlay plane in mirror mode\n");
+    } else if (!b->sprites_are_broken && !output->virtual) {
         drm_debug(b, "\t[repaint] trying planes-only build state\n");
         state = drm_output_propose_state(output_base, pending_state, mode);
         if (!state) {
diff --git a/libweston/compositor.c b/libweston/compositor.c
index 22a8593..7680445 100644
--- a/libweston/compositor.c
+++ b/libweston/compositor.c
@@ -1407,7 +1407,7 @@ weston_view_assign_output(struct weston_view *ev)
     mask = 0;
     pixman_region32_init(&region);
     wl_list_for_each(output, &ec->output_list, link) {
-        if (output->destroying)
+        if (!weston_output_valid(output))
             continue;
 
         pixman_region32_intersect(&region, &ev->transform.boundingbox,
@@ -5249,6 +5249,9 @@ bind_output(struct wl_client *client,
 static void
 weston_head_add_global(struct weston_head *head)
 {
+    if (head->global || !weston_output_valid(head->output))
+        return;
+
     head->global = wl_global_create(head->compositor->wl_display,
                     &wl_output_interface, 3,
                     head, bind_output);
@@ -5284,6 +5287,15 @@ weston_head_remove_global(struct weston_head *head)
     wl_list_init(&head->xdg_output_resource_list);
 }
 
+static void
+weston_head_update_global(struct weston_head *head)
+{
+    if (weston_output_valid(head->output))
+        weston_head_add_global(head);
+    else
+        weston_head_remove_global(head);
+}
+
 /** Get the backing object of wl_output
  *
  * \param resource A wl_output protocol object.
@@ -6079,11 +6091,15 @@ WL_EXPORT void
 weston_compositor_reflow_outputs(struct weston_compositor *compositor)
 {
     struct weston_output *output;
+    struct weston_head *head;
     int x, y, next_x, next_y;
 
     next_x = next_y = 0;
     wl_list_for_each(output, &compositor->output_list, link) {
-        if (output->destroying)
+        wl_list_for_each(head, &output->head_list, output_link)
+            weston_head_update_global(head);
+
+        if (!weston_output_valid(output))
             continue;
 
         x = next_x;
@@ -6300,11 +6316,11 @@ weston_compositor_add_output(struct weston_compositor *compositor,
     wl_list_insert(compositor->output_list.prev, &output->link);
     output->enabled = true;
 
+    wl_signal_emit(&compositor->output_created_signal, output);
+
     wl_list_for_each(head, &output->head_list, output_link)
         weston_head_add_global(head);
 
-    wl_signal_emit(&compositor->output_created_signal, output);
-
     /*
      * Use view_list, as paint nodes have not been created for this
      * output yet. Any existing view might touch this new output.
diff --git a/libweston/input.c b/libweston/input.c
index 6fb4bed..ec7e416 100644
--- a/libweston/input.c
+++ b/libweston/input.c
@@ -1688,6 +1688,10 @@ weston_pointer_clamp(struct weston_pointer *pointer, wl_fixed_t *fx, wl_fixed_t
     wl_list_for_each(output, &ec->output_list, link) {
         if (pointer->seat->output && pointer->seat->output != output)
             continue;
+
+        if (output->unavailable)
+            continue;
+
         if (pixman_region32_contains_point(&output->region,
                            x, y, NULL))
             valid = 1;
@@ -1757,6 +1761,9 @@ weston_pointer_handle_output_destroy(struct wl_listener *listener, void *data)
     y = wl_fixed_to_int(pointer->y);
 
     wl_list_for_each(output, &ec->output_list, link) {
+        if (output->unavailable)
+            continue;
+
         if (pixman_region32_contains_point(&output->region,
                            x, y, NULL))
             return;
diff --git a/meson.build b/meson.build
index 0d53c1b..c9c58e0 100644
--- a/meson.build
+++ b/meson.build
@@ -145,6 +145,11 @@ if get_option('deprecated-wl-shell')
     config_h.set('HAVE_DEPRECATED_WL_SHELL', '1')
 endif
 
+dep_rga = dependency('librga', required: false)
+if dep_rga.found()
+    config_h.set('HAVE_RGA', '1')
+endif
+
 dep_wayland_server = dependency('wayland-server', version: '>= 1.18.0')
 dep_wayland_client = dependency('wayland-client', version: '>= 1.18.0')
 dep_pixman = dependency('pixman-1', version: '>= 0.25.2')
-- 
2.20.1