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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
|
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:expandtab:shiftwidth=2:tabstop=2:
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* MozContainerWayland is a wrapper over MozContainer which provides
* wl_surface for MozContainer widget.
*
* The widget scheme looks like:
*
* ---------------------------------------------------------
* | mShell Gtk widget (contains wl_surface owned by Gtk+) |
* | |
* | --------------------------------------------------- |
* | | mContainer (contains wl_surface owned by Gtk+) | |
* | | | |
* | | --------------------------------------------- | |
* | | | wl_subsurface (attached to wl_surface | | |
* | | | of mContainer) | | |
* | | | | | |
* | | | | | |
* | | --------------------------------------------- | |
* | --------------------------------------------------- |
* ---------------------------------------------------------
*
* We draw to wl_subsurface owned by MozContainerWayland.
* We need to wait until wl_surface of mContainer is created
* and then we create and attach our wl_subsurface to it.
*
* First wl_subsurface creation has these steps:
*
* 1) moz_container_wayland_size_allocate() handler is called when
* mContainer size/position is known.
* It calls moz_container_wayland_surface_create_locked(), registers
* a frame callback handler
* moz_container_wayland_frame_callback_handler().
*
* 2) moz_container_wayland_frame_callback_handler() is called
* when wl_surface owned by mozContainer is ready.
* We call initial_draw_cbs() handler and we can create our wl_subsurface
* on top of wl_surface owned by mozContainer.
*
* When MozContainer hides/show again, moz_container_wayland_size_allocate()
* handler may not be called as MozContainer size is set. So after first
* show/hide sequence use moz_container_wayland_map_event() to create
* wl_subsurface of MozContainer.
*/
#include "MozContainer.h"
#include <dlfcn.h>
#include <glib.h>
#include <stdio.h>
#include <wayland-egl.h>
#include "mozilla/gfx/gfxVars.h"
#include "mozilla/StaticPrefs_widget.h"
#include "nsGtkUtils.h"
#include "nsWaylandDisplay.h"
#include "base/task.h"
#ifdef MOZ_LOGGING
# include "mozilla/Logging.h"
# include "nsTArray.h"
# include "Units.h"
# include "nsWindow.h"
extern mozilla::LazyLogModule gWidgetWaylandLog;
extern mozilla::LazyLogModule gWidgetLog;
# define LOGWAYLAND(...) \
MOZ_LOG(gWidgetWaylandLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
# define LOGCONTAINER(...) \
MOZ_LOG(gWidgetLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
#else
# define LOGWAYLAND(...)
# define LOGCONTAINER(...)
#endif /* MOZ_LOGGING */
using namespace mozilla;
using namespace mozilla::widget;
static bool moz_container_wayland_surface_create_locked(
const MutexAutoLock& aProofOfLock, MozContainer* container);
static void moz_container_wayland_set_opaque_region_locked(
const MutexAutoLock& aProofOfLock, MozContainer* container);
// Lock mozcontainer and get wayland surface of it. You need to pair with
// moz_container_wayland_surface_unlock() even
// if moz_container_wayland_surface_lock() fails and returns nullptr.
static struct wl_surface* moz_container_wayland_surface_lock(
MozContainer* container);
static void moz_container_wayland_surface_unlock(MozContainer* container,
struct wl_surface** surface);
MozContainerSurfaceLock::MozContainerSurfaceLock(MozContainer* aContainer) {
mContainer = aContainer;
mSurface = moz_container_wayland_surface_lock(aContainer);
}
MozContainerSurfaceLock::~MozContainerSurfaceLock() {
moz_container_wayland_surface_unlock(mContainer, &mSurface);
}
struct wl_surface* MozContainerSurfaceLock::GetSurface() { return mSurface; }
// Invalidate gtk wl_surface to commit changes to wl_subsurface.
// wl_subsurface changes are effective when parent surface is commited.
static void moz_container_wayland_invalidate(MozContainer* container) {
LOGWAYLAND("moz_container_wayland_invalidate [%p]\n",
(void*)moz_container_get_nsWindow(container));
GdkWindow* window = gtk_widget_get_window(GTK_WIDGET(container));
if (!window) {
LOGWAYLAND(" Failed - missing GdkWindow!\n");
return;
}
gdk_window_invalidate_rect(window, nullptr, true);
}
// Route input to parent wl_surface owned by Gtk+ so we get input
// events from Gtk+.
static void moz_container_clear_input_region(MozContainer* container) {
struct wl_compositor* compositor = WaylandDisplayGet()->GetCompositor();
MozContainerWayland* wl_container = &container->data.wl_container;
wl_region* region = wl_compositor_create_region(compositor);
wl_surface_set_input_region(wl_container->surface, region);
wl_region_destroy(region);
}
static void moz_container_wayland_move_locked(const MutexAutoLock& aProofOfLock,
MozContainer* container, int dx,
int dy) {
LOGCONTAINER("moz_container_wayland_move [%p] %d,%d\n",
(void*)moz_container_get_nsWindow(container), dx, dy);
MozContainerWayland* wl_container = &container->data.wl_container;
if (!wl_container->subsurface || (wl_container->subsurface_dx == dx &&
wl_container->subsurface_dy == dy)) {
return;
}
wl_container->subsurface_dx = dx;
wl_container->subsurface_dy = dy;
wl_subsurface_set_position(wl_container->subsurface,
wl_container->subsurface_dx,
wl_container->subsurface_dy);
}
// This is called from layout/compositor code only with
// size equal to GL rendering context.
// Return false if scale factor doesn't match buffer size.
// We need to skip painting in such case do avoid Wayland compositor freaking.
bool moz_container_wayland_egl_window_set_size(MozContainer* container,
nsIntSize aSize, int aScale) {
MozContainerWayland* wl_container = &container->data.wl_container;
MutexAutoLock lock(wl_container->container_lock);
// We may be called after unmap so we're missing egl window completelly.
// In such case don't return false which would block compositor.
// We return true here and don't block flush WebRender queue.
// We'll be repainted if our window become visible again anyway.
if (!wl_container->eglwindow) {
return true;
}
if (wl_container->buffer_scale != aScale) {
moz_container_wayland_set_scale_factor_locked(lock, container, aScale);
}
/* Enable for size changes logging
LOGCONTAINER(
"moz_container_wayland_egl_window_set_size [%p] %d x %d scale %d "
"(unscaled %d x %d)",
(void*)moz_container_get_nsWindow(container), aSize.width, aSize.height,
aScale, aSize.width / aScale, aSize.height / aScale);
*/
wl_egl_window_resize(wl_container->eglwindow, aSize.width, aSize.height, 0,
0);
return moz_container_wayland_size_matches_scale_factor_locked(
lock, container, aSize.width, aSize.height);
}
void moz_container_wayland_add_initial_draw_callback_locked(
MozContainer* container, const std::function<void(void)>& initial_draw_cb) {
MozContainerWayland* wl_container = &container->data.wl_container;
if (wl_container->ready_to_draw && !wl_container->surface) {
NS_WARNING(
"moz_container_wayland_add_or_fire_initial_draw_callback:"
" ready to draw without wayland surface!");
}
MOZ_DIAGNOSTIC_ASSERT(!wl_container->ready_to_draw || !wl_container->surface);
wl_container->initial_draw_cbs.push_back(initial_draw_cb);
}
void moz_container_wayland_add_or_fire_initial_draw_callback(
MozContainer* container, const std::function<void(void)>& initial_draw_cb) {
MozContainerWayland* wl_container = &container->data.wl_container;
{
MutexAutoLock lock(wl_container->container_lock);
if (wl_container->ready_to_draw && !wl_container->surface) {
NS_WARNING(
"moz_container_wayland_add_or_fire_initial_draw_callback: ready to "
"draw "
"without wayland surface!");
}
if (!wl_container->ready_to_draw || !wl_container->surface) {
wl_container->initial_draw_cbs.push_back(initial_draw_cb);
return;
}
}
// We're ready to draw as
// wl_container->ready_to_draw && wl_container->surface
// call the callback directly instead of store them.
initial_draw_cb();
}
static void moz_container_wayland_clear_initial_draw_callback_locked(
const MutexAutoLock& aProofOfLock, MozContainer* container) {
MozContainerWayland* wl_container = &container->data.wl_container;
MozClearPointer(wl_container->frame_callback_handler, wl_callback_destroy);
wl_container->initial_draw_cbs.clear();
}
void moz_container_wayland_clear_initial_draw_callback(
MozContainer* container) {
MutexAutoLock lock(container->data.wl_container.container_lock);
moz_container_wayland_clear_initial_draw_callback_locked(lock, container);
}
static void moz_container_wayland_frame_callback_handler(
void* data, struct wl_callback* callback, uint32_t time) {
MozContainerWayland* wl_container = MOZ_WL_CONTAINER(data);
LOGWAYLAND(
"%s [%p] frame_callback_handler %p ready_to_draw %d (set to true)"
" initial_draw callback %zd\n",
__FUNCTION__, (void*)moz_container_get_nsWindow(MOZ_CONTAINER(data)),
(void*)wl_container->frame_callback_handler, wl_container->ready_to_draw,
wl_container->initial_draw_cbs.size());
std::vector<std::function<void(void)>> cbs;
{
// Protect mozcontainer internals changes by container_lock.
MutexAutoLock lock(wl_container->container_lock);
MozClearPointer(wl_container->frame_callback_handler, wl_callback_destroy);
// It's possible that container is already unmapped so quit in such case.
if (!wl_container->surface) {
LOGWAYLAND(" container is unmapped, quit.");
if (!wl_container->initial_draw_cbs.empty()) {
NS_WARNING("Unmapping MozContainer with active draw callback!");
wl_container->initial_draw_cbs.clear();
}
return;
}
if (wl_container->ready_to_draw) {
return;
}
wl_container->ready_to_draw = true;
cbs = std::move(wl_container->initial_draw_cbs);
}
// Call the callbacks registered by
// moz_container_wayland_add_or_fire_initial_draw_callback().
// and we can't do that under mozcontainer lock.
for (auto const& cb : cbs) {
cb();
}
}
static const struct wl_callback_listener moz_container_frame_listener = {
moz_container_wayland_frame_callback_handler};
static void after_frame_clock_after_paint(GdkFrameClock* clock,
MozContainer* container) {
MozContainerSurfaceLock lock(container);
struct wl_surface* surface = lock.GetSurface();
if (surface) {
wl_surface_commit(surface);
}
}
static bool moz_gdk_wayland_window_add_frame_callback_surface_locked(
const MutexAutoLock& aProofOfLock, MozContainer* container) {
static auto sGdkWaylandWindowAddCallbackSurface =
(void (*)(GdkWindow*, struct wl_surface*))dlsym(
RTLD_DEFAULT, "gdk_wayland_window_add_frame_callback_surface");
if (!StaticPrefs::widget_wayland_opaque_region_enabled_AtStartup() ||
!sGdkWaylandWindowAddCallbackSurface) {
return false;
}
GdkWindow* window = gtk_widget_get_window(GTK_WIDGET(container));
MozContainerWayland* wl_container = &container->data.wl_container;
sGdkWaylandWindowAddCallbackSurface(window, wl_container->surface);
GdkFrameClock* frame_clock = gdk_window_get_frame_clock(window);
g_signal_connect_after(frame_clock, "after-paint",
G_CALLBACK(after_frame_clock_after_paint), container);
return true;
}
static void moz_gdk_wayland_window_remove_frame_callback_surface_locked(
const MutexAutoLock& aProofOfLock, MozContainer* container) {
static auto sGdkWaylandWindowRemoveCallbackSurface =
(void (*)(GdkWindow*, struct wl_surface*))dlsym(
RTLD_DEFAULT, "gdk_wayland_window_remove_frame_callback_surface");
if (!sGdkWaylandWindowRemoveCallbackSurface) {
return;
}
GdkWindow* window = gtk_widget_get_window(GTK_WIDGET(container));
MozContainerWayland* wl_container = &container->data.wl_container;
if (wl_container->surface) {
sGdkWaylandWindowRemoveCallbackSurface(window, wl_container->surface);
}
GdkFrameClock* frame_clock = gdk_window_get_frame_clock(window);
g_signal_handlers_disconnect_by_func(
frame_clock, FuncToGpointer(after_frame_clock_after_paint), container);
}
void moz_container_wayland_unmap(GtkWidget* widget) {
g_return_if_fail(IS_MOZ_CONTAINER(widget));
// Unmap MozContainer first so we can remove our resources
moz_container_unmap(widget);
MozContainer* container = MOZ_CONTAINER(widget);
MozContainerWayland* wl_container = &container->data.wl_container;
MutexAutoLock lock(wl_container->container_lock);
LOGCONTAINER("%s [%p]\n", __FUNCTION__,
(void*)moz_container_get_nsWindow(container));
moz_container_wayland_clear_initial_draw_callback_locked(lock, container);
if (wl_container->opaque_region_used) {
moz_gdk_wayland_window_remove_frame_callback_surface_locked(lock,
container);
}
if (wl_container->commit_to_parent) {
wl_container->surface = nullptr;
}
MozClearPointer(wl_container->eglwindow, wl_egl_window_destroy);
MozClearPointer(wl_container->subsurface, wl_subsurface_destroy);
MozClearPointer(wl_container->surface, wl_surface_destroy);
MozClearPointer(wl_container->viewport, wp_viewport_destroy);
MozClearPointer(wl_container->fractional_scale,
wp_fractional_scale_v1_destroy);
wl_container->ready_to_draw = false;
wl_container->buffer_scale = 1;
wl_container->current_fractional_scale = 0.0;
}
gboolean moz_container_wayland_map_event(GtkWidget* widget,
GdkEventAny* event) {
MozContainerWayland* wl_container = &MOZ_CONTAINER(widget)->data.wl_container;
LOGCONTAINER("%s [%p]\n", __FUNCTION__,
(void*)moz_container_get_nsWindow(MOZ_CONTAINER(widget)));
// Return early if we're not mapped. Gtk may send bogus map_event signal
// to unmapped widgets (see Bug 1875369).
if (!gtk_widget_get_mapped(widget)) {
return false;
}
// Make sure we're on main thread as we can't lock mozContainer here
// due to moz_container_wayland_add_or_fire_initial_draw_callback() call
// below.
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
// Set waiting_to_show flag. It means the mozcontainer is cofigured/mapped
// and it's supposed to be visible. *But* it's really visible when we get
// moz_container_wayland_add_or_fire_initial_draw_callback() which means
// wayland compositor makes it live.
wl_container->waiting_to_show = true;
MozContainer* container = MOZ_CONTAINER(widget);
moz_container_wayland_add_or_fire_initial_draw_callback(
container, [container]() -> void {
LOGCONTAINER(
"[%p] moz_container_wayland_add_or_fire_initial_draw_callback set "
"visible",
moz_container_get_nsWindow(container));
moz_container_wayland_clear_waiting_to_show_flag(container);
});
MutexAutoLock lock(wl_container->container_lock);
// Don't create wl_subsurface in map_event when it's already created or
// if we create it for the first time.
if (wl_container->ready_to_draw || wl_container->before_first_size_alloc) {
return FALSE;
}
if (!wl_container->surface) {
if (!moz_container_wayland_surface_create_locked(lock,
MOZ_CONTAINER(widget))) {
return FALSE;
}
}
nsWindow* window = moz_container_get_nsWindow(MOZ_CONTAINER(widget));
moz_container_wayland_set_scale_factor_locked(lock, MOZ_CONTAINER(widget),
window->GdkCeiledScaleFactor());
moz_container_wayland_set_opaque_region_locked(lock, MOZ_CONTAINER(widget));
moz_container_clear_input_region(MOZ_CONTAINER(widget));
moz_container_wayland_invalidate(MOZ_CONTAINER(widget));
return FALSE;
}
void moz_container_wayland_size_allocate(GtkWidget* widget,
GtkAllocation* allocation) {
GtkAllocation tmp_allocation;
g_return_if_fail(IS_MOZ_CONTAINER(widget));
LOGCONTAINER("moz_container_wayland_size_allocate [%p] %d,%d -> %d x %d\n",
(void*)moz_container_get_nsWindow(MOZ_CONTAINER(widget)),
allocation->x, allocation->y, allocation->width,
allocation->height);
/* short circuit if you can */
gtk_widget_get_allocation(widget, &tmp_allocation);
if (tmp_allocation.x == allocation->x && tmp_allocation.y == allocation->y &&
tmp_allocation.width == allocation->width &&
tmp_allocation.height == allocation->height) {
return;
}
gtk_widget_set_allocation(widget, allocation);
if (gtk_widget_get_has_window(widget) && gtk_widget_get_realized(widget)) {
gdk_window_move_resize(gtk_widget_get_window(widget), allocation->x,
allocation->y, allocation->width,
allocation->height);
// We need to position our subsurface according to GdkWindow
// when offset changes (GdkWindow is maximized for instance).
// see gtk-clutter-embed.c for reference.
MozContainer* container = MOZ_CONTAINER(widget);
MutexAutoLock lock(container->data.wl_container.container_lock);
if (!container->data.wl_container.surface) {
if (!moz_container_wayland_surface_create_locked(lock, container)) {
return;
}
}
nsWindow* window = moz_container_get_nsWindow(container);
moz_container_wayland_set_scale_factor_locked(
lock, container, window->GdkCeiledScaleFactor());
moz_container_wayland_set_opaque_region_locked(lock, container);
moz_container_wayland_move_locked(lock, container, allocation->x,
allocation->y);
moz_container_clear_input_region(container);
moz_container_wayland_invalidate(MOZ_CONTAINER(widget));
container->data.wl_container.before_first_size_alloc = false;
}
}
static wl_region* moz_container_wayland_create_opaque_region(
int aX, int aY, int aWidth, int aHeight, int aCornerRadius) {
struct wl_compositor* compositor = WaylandDisplayGet()->GetCompositor();
wl_region* region = wl_compositor_create_region(compositor);
wl_region_add(region, aX, aY, aWidth, aHeight);
if (aCornerRadius) {
wl_region_subtract(region, aX, aY, aCornerRadius, aCornerRadius);
wl_region_subtract(region, aX + aWidth - aCornerRadius, aY, aCornerRadius,
aCornerRadius);
wl_region_subtract(region, aX, aY + aHeight - aCornerRadius, aCornerRadius,
aCornerRadius);
wl_region_subtract(region, aX + aWidth - aCornerRadius,
aY + aHeight - aCornerRadius, aCornerRadius,
aCornerRadius);
}
return region;
}
static void moz_container_wayland_set_opaque_region_locked(
const MutexAutoLock& aProofOfLock, MozContainer* container) {
MozContainerWayland* wl_container = &container->data.wl_container;
if (!wl_container->opaque_region_needs_updates) {
return;
}
if (!wl_container->opaque_region_used) {
wl_container->opaque_region_needs_updates = false;
return;
}
GtkAllocation allocation;
gtk_widget_get_allocation(GTK_WIDGET(container), &allocation);
wl_region* region = moz_container_wayland_create_opaque_region(
0, 0, allocation.width, allocation.height,
wl_container->opaque_region_corner_radius);
wl_surface_set_opaque_region(wl_container->surface, region);
wl_region_destroy(region);
wl_container->opaque_region_needs_updates = false;
}
static void moz_container_wayland_set_opaque_region(MozContainer* container) {
MozContainerWayland* wl_container = &container->data.wl_container;
MutexAutoLock lock(wl_container->container_lock);
if (wl_container->surface) {
moz_container_wayland_set_opaque_region_locked(lock, container);
}
}
static void moz_container_wayland_surface_set_scale_locked(
const MutexAutoLock& aProofOfLock, MozContainerWayland* wl_container,
int scale) {
if (!wl_container->surface) {
return;
}
if (wl_container->buffer_scale == scale) {
return;
}
LOGCONTAINER("%s scale %d\n", __FUNCTION__, scale);
// There is a chance that the attached wl_buffer has not yet been doubled
// on the main thread when scale factor changed to 2. This leads to
// crash with the following message:
// Buffer size (AxB) must be an integer multiple of the buffer_scale (2)
// Removing the possibly wrong wl_buffer to prevent that crash:
wl_surface_attach(wl_container->surface, nullptr, 0, 0);
wl_surface_set_buffer_scale(wl_container->surface, scale);
wl_container->buffer_scale = scale;
}
static void fractional_scale_handle_preferred_scale(
void* data, struct wp_fractional_scale_v1* info, uint32_t wire_scale) {
MozContainer* container = MOZ_CONTAINER(data);
MozContainerWayland* wl_container = &container->data.wl_container;
wl_container->current_fractional_scale = wire_scale / 120.0;
RefPtr<nsWindow> window = moz_container_get_nsWindow(container);
LOGWAYLAND("%s [%p] scale: %f\n", __func__, window.get(),
wl_container->current_fractional_scale);
MOZ_DIAGNOSTIC_ASSERT(window);
window->OnScaleChanged(/* aNotify = */ true);
}
static const struct wp_fractional_scale_v1_listener fractional_scale_listener =
{
.preferred_scale = fractional_scale_handle_preferred_scale,
};
void moz_container_wayland_set_scale_factor_locked(
const MutexAutoLock& aProofOfLock, MozContainer* container, int aScale) {
if (gfx::gfxVars::UseWebRenderCompositor()) {
// the compositor backend handles scaling itself
return;
}
MozContainerWayland* wl_container = &container->data.wl_container;
wl_container->container_lock.AssertCurrentThreadOwns();
if (StaticPrefs::widget_wayland_fractional_scale_enabled_AtStartup()) {
if (!wl_container->fractional_scale) {
if (auto* manager = WaylandDisplayGet()->GetFractionalScaleManager()) {
wl_container->fractional_scale =
wp_fractional_scale_manager_v1_get_fractional_scale(
manager, wl_container->surface);
wp_fractional_scale_v1_add_listener(wl_container->fractional_scale,
&fractional_scale_listener,
container);
}
}
if (wl_container->fractional_scale) {
if (!wl_container->viewport) {
if (auto* viewporter = WaylandDisplayGet()->GetViewporter()) {
wl_container->viewport =
wp_viewporter_get_viewport(viewporter, wl_container->surface);
}
}
if (wl_container->viewport) {
GdkWindow* gdkWindow = gtk_widget_get_window(GTK_WIDGET(container));
wp_viewport_set_destination(wl_container->viewport,
gdk_window_get_width(gdkWindow),
gdk_window_get_height(gdkWindow));
return;
}
}
}
moz_container_wayland_surface_set_scale_locked(aProofOfLock, wl_container,
aScale);
}
bool moz_container_wayland_size_matches_scale_factor_locked(
const MutexAutoLock& aProofOfLock, MozContainer* container, int aWidth,
int aHeight) {
return aWidth % container->data.wl_container.buffer_scale == 0 &&
aHeight % container->data.wl_container.buffer_scale == 0;
}
static bool moz_container_wayland_surface_create_locked(
const MutexAutoLock& aProofOfLock, MozContainer* container) {
MozContainerWayland* wl_container = &container->data.wl_container;
LOGWAYLAND("%s [%p]\n", __FUNCTION__,
(void*)moz_container_get_nsWindow(container));
GdkWindow* window = gtk_widget_get_window(GTK_WIDGET(container));
MOZ_DIAGNOSTIC_ASSERT(window);
wl_surface* parent_surface = gdk_wayland_window_get_wl_surface(window);
if (!parent_surface) {
LOGWAYLAND(" Failed - missing parent surface!");
return false;
}
LOGWAYLAND(" gtk wl_surface %p ID %d\n", (void*)parent_surface,
wl_proxy_get_id((struct wl_proxy*)parent_surface));
if (wl_container->commit_to_parent) {
LOGWAYLAND(" commit to parent");
wl_container->surface = parent_surface;
NS_DispatchToCurrentThread(NewRunnableFunction(
"moz_container_wayland_frame_callback_handler",
&moz_container_wayland_frame_callback_handler, container, nullptr, 0));
return true;
}
// Available as of GTK 3.8+
struct wl_compositor* compositor = WaylandDisplayGet()->GetCompositor();
wl_container->surface = wl_compositor_create_surface(compositor);
if (!wl_container->surface) {
LOGWAYLAND(" Failed - can't create surface!");
return false;
}
wl_container->subsurface =
wl_subcompositor_get_subsurface(WaylandDisplayGet()->GetSubcompositor(),
wl_container->surface, parent_surface);
if (!wl_container->subsurface) {
MozClearPointer(wl_container->surface, wl_surface_destroy);
LOGWAYLAND(" Failed - can't create sub-surface!");
return false;
}
wl_subsurface_set_desync(wl_container->subsurface);
// Try to guess subsurface offset to avoid potential flickering.
int dx, dy;
if (moz_container_get_nsWindow(container)->GetCSDDecorationOffset(&dx, &dy)) {
wl_container->subsurface_dx = dx;
wl_container->subsurface_dy = dy;
wl_subsurface_set_position(wl_container->subsurface, dx, dy);
LOGWAYLAND(" guessing subsurface position %d %d\n", dx, dy);
}
// If there's pending frame callback it's for wrong parent surface,
// so delete it.
if (wl_container->frame_callback_handler) {
MozClearPointer(wl_container->frame_callback_handler, wl_callback_destroy);
}
wl_container->frame_callback_handler = wl_surface_frame(parent_surface);
wl_callback_add_listener(wl_container->frame_callback_handler,
&moz_container_frame_listener, container);
LOGWAYLAND(
" created frame callback ID %d\n",
wl_proxy_get_id((struct wl_proxy*)wl_container->frame_callback_handler));
wl_surface_commit(wl_container->surface);
wl_display_flush(WaylandDisplayGet()->GetDisplay());
wl_container->opaque_region_used =
moz_gdk_wayland_window_add_frame_callback_surface_locked(aProofOfLock,
container);
LOGWAYLAND(" created surface %p ID %d\n", (void*)wl_container->surface,
wl_proxy_get_id((struct wl_proxy*)wl_container->surface));
return true;
}
struct wl_surface* moz_container_wayland_surface_lock(MozContainer* container)
MOZ_NO_THREAD_SAFETY_ANALYSIS {
// LOGWAYLAND("%s [%p] surface %p ready_to_draw %d\n", __FUNCTION__,
// (void*)container, (void*)container->data.wl_container.surface,
// container->data.wl_container.ready_to_draw);
container->data.wl_container.container_lock.Lock();
if (!container->data.wl_container.surface ||
!container->data.wl_container.ready_to_draw) {
return nullptr;
}
return container->data.wl_container.surface;
}
void moz_container_wayland_surface_unlock(MozContainer* container,
struct wl_surface** surface)
MOZ_NO_THREAD_SAFETY_ANALYSIS {
// Temporarily disabled to avoid log noise
// LOGWAYLAND("%s [%p] surface %p\n", __FUNCTION__, (void*)container,
// (void*)container->data.wl_container.surface);
if (*surface) {
*surface = nullptr;
}
container->data.wl_container.container_lock.Unlock();
}
struct wl_egl_window* moz_container_wayland_get_egl_window(
MozContainer* container, double scale) {
MozContainerWayland* wl_container = &container->data.wl_container;
LOGCONTAINER("%s [%p] eglwindow %p scale %d\n", __FUNCTION__,
(void*)moz_container_get_nsWindow(container),
(void*)wl_container->eglwindow, (int)scale);
MutexAutoLock lock(wl_container->container_lock);
if (!wl_container->surface || !wl_container->ready_to_draw) {
LOGCONTAINER(
" quit, wl_container->surface %p wl_container->ready_to_draw %d\n",
wl_container->surface, wl_container->ready_to_draw);
return nullptr;
}
GdkWindow* window = gtk_widget_get_window(GTK_WIDGET(container));
nsIntSize requestedSize((int)round(gdk_window_get_width(window) * scale),
(int)round(gdk_window_get_height(window) * scale));
if (!wl_container->eglwindow) {
wl_container->eglwindow = wl_egl_window_create(
wl_container->surface, requestedSize.width, requestedSize.height);
LOGCONTAINER("%s [%p] created eglwindow %p size %d x %d (with scale %f)\n",
__FUNCTION__, (void*)moz_container_get_nsWindow(container),
(void*)wl_container->eglwindow, requestedSize.width,
requestedSize.height, scale);
} else {
nsIntSize recentSize;
wl_egl_window_get_attached_size(wl_container->eglwindow, &recentSize.width,
&recentSize.height);
if (requestedSize != recentSize) {
LOGCONTAINER("%s [%p] resized to %d x %d (with scale %f)\n", __FUNCTION__,
(void*)moz_container_get_nsWindow(container),
requestedSize.width, requestedSize.height, scale);
wl_egl_window_resize(wl_container->eglwindow, requestedSize.width,
requestedSize.height, 0, 0);
}
}
moz_container_wayland_surface_set_scale_locked(lock, wl_container,
static_cast<int>(scale));
return wl_container->eglwindow;
}
gboolean moz_container_wayland_has_egl_window(MozContainer* container) {
return !!container->data.wl_container.eglwindow;
}
void moz_container_wayland_update_opaque_region(MozContainer* container,
int corner_radius) {
MozContainerWayland* wl_container = &container->data.wl_container;
wl_container->opaque_region_needs_updates = true;
wl_container->opaque_region_corner_radius = corner_radius;
// When GL compositor / WebRender is used,
// moz_container_wayland_get_egl_window() is called only once when window
// is created or resized so update opaque region now.
if (moz_container_wayland_has_egl_window(container)) {
moz_container_wayland_set_opaque_region(container);
}
}
gboolean moz_container_wayland_can_draw(MozContainer* container) {
MozContainerWayland* wl_container = &container->data.wl_container;
MutexAutoLock lock(wl_container->container_lock);
return wl_container->ready_to_draw;
}
double moz_container_wayland_get_fractional_scale(MozContainer* container) {
return container->data.wl_container.current_fractional_scale;
}
double moz_container_wayland_get_scale(MozContainer* container) {
nsWindow* window = moz_container_get_nsWindow(container);
return window ? window->FractionalScaleFactor() : 1.0;
}
void moz_container_wayland_set_commit_to_parent(MozContainer* container) {
MozContainerWayland* wl_container = &container->data.wl_container;
MOZ_DIAGNOSTIC_ASSERT(!wl_container->surface);
wl_container->commit_to_parent = true;
}
bool moz_container_wayland_is_commiting_to_parent(MozContainer* container) {
return container->data.wl_container.commit_to_parent;
}
bool moz_container_wayland_is_waiting_to_show(MozContainer* container) {
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
return container->data.wl_container.waiting_to_show;
}
void moz_container_wayland_clear_waiting_to_show_flag(MozContainer* container) {
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
container->data.wl_container.waiting_to_show = false;
}
|