Bug 1628583 [Wayland] Use mutex to access mozcontainer wayland internals, r=jhorak

Mozcontainer is accessed from various threads - Main/Compositor/Renderer - which causes races
when wayland surfaces are created/deleted/updated so we need to control it.

Differential Revision: https://phabricator.services.mozilla.com/D71737
This commit is contained in:
Martin Stransky 2020-04-22 15:53:49 +00:00
parent cb0716453c
commit f5dbae2f0c
2 changed files with 74 additions and 20 deletions

View File

@ -41,9 +41,7 @@ extern mozilla::LazyLogModule gWidgetWaylandLog;
#ifdef MOZ_WAYLAND
using namespace mozilla;
using namespace mozilla::widget;
#endif
#ifdef MOZ_WAYLAND
// Declaration from nsWindow, we don't want to include whole nsWindow.h file
// here just for it.
wl_region* CreateOpaqueRegionWayland(int aX, int aY, int aWidth, int aHeight,
@ -53,6 +51,9 @@ wl_region* CreateOpaqueRegionWayland(int aX, int aY, int aWidth, int aHeight,
/* init methods */
static void moz_container_class_init(MozContainerClass* klass);
static void moz_container_init(MozContainer* container);
#if defined(MOZ_WAYLAND)
static void moz_container_destroy(GtkWidget* widget);
#endif
/* widget class methods */
static void moz_container_map(GtkWidget* widget);
@ -150,8 +151,9 @@ void moz_container_put(MozContainer* container, GtkWidget* child_widget, gint x,
}
#if defined(MOZ_WAYLAND)
void moz_container_move(MozContainer* container, int dx, int dy) {
LOGWAYLAND(("moz_container_move [%p] %d,%d\n", (void*)container, dx, dy));
static void moz_container_move_locked(MozContainer* container, int dx, int dy) {
LOGWAYLAND(
("moz_container_move_locked [%p] %d,%d\n", (void*)container, dx, dy));
container->subsurface_dx = dx;
container->subsurface_dy = dy;
@ -176,12 +178,19 @@ void moz_container_move(MozContainer* container, int dx, int dy) {
}
}
static void moz_container_move(MozContainer* container, int dx, int dy) {
MutexAutoLock lock(*container->container_lock);
LOGWAYLAND(("moz_container_move [%p] %d,%d\n", (void*)container, dx, dy));
moz_container_move_locked(container, dx, dy);
}
// This is called from layout/compositor code only with
// size equal to GL rendering context. Otherwise there are
// rendering artifacts as wl_egl_window size does not match
// GL rendering pipeline setup.
void moz_container_egl_window_set_size(MozContainer* container, int width,
int height) {
MutexAutoLock lock(*container->container_lock);
if (container->eglwindow) {
wl_egl_window_resize(container->eglwindow, width, height, 0, 0);
}
@ -198,6 +207,7 @@ void moz_container_class_init(MozContainerClass* klass) {
#if defined(MOZ_WAYLAND)
if (gfxPlatformGtk::GetPlatform()->IsWaylandDisplay()) {
widget_class->map_event = moz_container_map_wayland;
widget_class->destroy = moz_container_destroy;
}
#endif
widget_class->unmap = moz_container_unmap;
@ -230,12 +240,21 @@ void moz_container_init(MozContainer* container) {
container->subsurface_dy = 0;
container->surface_position_needs_update = 0;
container->initial_draw_cbs.clear();
if (gfxPlatformGtk::GetPlatform()->IsWaylandDisplay()) {
container->container_lock = new mozilla::Mutex("MozContainer lock");
}
#endif
LOG(("%s [%p]\n", __FUNCTION__, (void*)container));
}
#if defined(MOZ_WAYLAND)
static void moz_container_destroy(GtkWidget* widget) {
MozContainer* container = MOZ_CONTAINER(widget);
delete container->container_lock;
container->container_lock = nullptr;
}
void moz_container_add_initial_draw_callback(
MozContainer* container, const std::function<void(void)>& initial_draw_cb) {
container->initial_draw_cbs.push_back(initial_draw_cb);
@ -333,6 +352,8 @@ static gboolean moz_container_map_wayland(GtkWidget* widget,
}
static void moz_container_unmap_wayland(MozContainer* container) {
MutexAutoLock lock(*container->container_lock);
g_clear_pointer(&container->eglwindow, wl_egl_window_destroy);
g_clear_pointer(&container->subsurface, wl_subsurface_destroy);
g_clear_pointer(&container->surface, wl_surface_destroy);
@ -573,7 +594,7 @@ static void moz_container_add(GtkContainer* container, GtkWidget* widget) {
}
#ifdef MOZ_WAYLAND
static void moz_container_set_opaque_region(MozContainer* container) {
static void moz_container_set_opaque_region_locked(MozContainer* container) {
if (!container->opaque_region_needs_update || !container->surface) {
return;
}
@ -595,6 +616,11 @@ static void moz_container_set_opaque_region(MozContainer* container) {
container->opaque_region_needs_update = false;
}
void moz_container_set_opaque_region(MozContainer* container) {
MutexAutoLock lock(*container->container_lock);
moz_container_set_opaque_region_locked(container);
}
static int moz_gtk_widget_get_scale_factor(MozContainer* container) {
static auto sGtkWidgetGetScaleFactor =
(gint(*)(GtkWidget*))dlsym(RTLD_DEFAULT, "gtk_widget_get_scale_factor");
@ -603,7 +629,7 @@ static int moz_gtk_widget_get_scale_factor(MozContainer* container) {
: 1;
}
void moz_container_set_scale_factor(MozContainer* container) {
static void moz_container_set_scale_factor_locked(MozContainer* container) {
if (!container->surface) {
return;
}
@ -611,7 +637,13 @@ void moz_container_set_scale_factor(MozContainer* container) {
moz_gtk_widget_get_scale_factor(container));
}
struct wl_surface* moz_container_get_wl_surface(MozContainer* container) {
void moz_container_set_scale_factor(MozContainer* container) {
MutexAutoLock lock(*container->container_lock);
moz_container_set_scale_factor_locked(container);
}
static struct wl_surface* moz_container_get_wl_surface_locked(
MozContainer* container, nsWaylandDisplay* aWaylandDisplay) {
LOGWAYLAND(("%s [%p] surface %p ready_to_draw %d\n", __FUNCTION__,
(void*)container, (void*)container->surface,
container->ready_to_draw));
@ -621,9 +653,6 @@ struct wl_surface* moz_container_get_wl_surface(MozContainer* container) {
moz_container_request_parent_frame_callback(container);
return nullptr;
}
GdkDisplay* display = gtk_widget_get_display(GTK_WIDGET(container));
nsWaylandDisplay* waylandDisplay = WaylandDisplayGet(display);
wl_surface* parent_surface =
moz_gtk_widget_get_wl_surface(GTK_WIDGET(container));
if (!parent_surface) {
@ -631,14 +660,15 @@ struct wl_surface* moz_container_get_wl_surface(MozContainer* container) {
}
// Available as of GTK 3.8+
struct wl_compositor* compositor = waylandDisplay->GetCompositor();
struct wl_compositor* compositor = aWaylandDisplay->GetCompositor();
container->surface = wl_compositor_create_surface(compositor);
if (!container->surface) {
return nullptr;
}
container->subsurface = wl_subcompositor_get_subsurface(
waylandDisplay->GetSubcompositor(), container->surface, parent_surface);
container->subsurface =
wl_subcompositor_get_subsurface(aWaylandDisplay->GetSubcompositor(),
container->surface, parent_surface);
if (!container->subsurface) {
g_clear_pointer(&container->surface, wl_surface_destroy);
return nullptr;
@ -647,7 +677,7 @@ struct wl_surface* moz_container_get_wl_surface(MozContainer* container) {
GdkWindow* window = gtk_widget_get_window(GTK_WIDGET(container));
gint x, y;
gdk_window_get_position(window, &x, &y);
moz_container_move(container, x, y);
moz_container_move_locked(container, x, y);
wl_subsurface_set_desync(container->subsurface);
// Route input to parent wl_surface owned by Gtk+ so we get input
@ -657,31 +687,48 @@ struct wl_surface* moz_container_get_wl_surface(MozContainer* container) {
wl_region_destroy(region);
wl_surface_commit(container->surface);
wl_display_flush(waylandDisplay->GetDisplay());
wl_display_flush(aWaylandDisplay->GetDisplay());
LOGWAYLAND(("%s [%p] created surface %p\n", __FUNCTION__, (void*)container,
(void*)container->surface));
}
if (container->surface_position_needs_update) {
moz_container_move(container, container->subsurface_dx,
container->subsurface_dy);
moz_container_move_locked(container, container->subsurface_dx,
container->subsurface_dy);
}
moz_container_set_opaque_region(container);
moz_container_set_scale_factor(container);
moz_container_set_opaque_region_locked(container);
moz_container_set_scale_factor_locked(container);
return container->surface;
}
struct wl_surface* moz_container_get_wl_surface(MozContainer* container) {
GdkDisplay* display = gtk_widget_get_display(GTK_WIDGET(container));
nsWaylandDisplay* waylandDisplay = WaylandDisplayGet(display);
LOGWAYLAND(("%s [%p] surface %p\n", __FUNCTION__, (void*)container,
(void*)container->surface));
MutexAutoLock lock(*container->container_lock);
return moz_container_get_wl_surface_locked(container, waylandDisplay);
}
struct wl_egl_window* moz_container_get_wl_egl_window(MozContainer* container,
int scale) {
GdkDisplay* display = gtk_widget_get_display(GTK_WIDGET(container));
nsWaylandDisplay* waylandDisplay = WaylandDisplayGet(display);
LOGWAYLAND(("%s [%p] eglwindow %p\n", __FUNCTION__, (void*)container,
(void*)container->eglwindow));
MutexAutoLock lock(*container->container_lock);
// Always call moz_container_get_wl_surface() to ensure underlying
// container->surface has correct scale and position.
wl_surface* surface = moz_container_get_wl_surface(container);
wl_surface* surface =
moz_container_get_wl_surface_locked(container, waylandDisplay);
if (!surface) {
return nullptr;
}

View File

@ -11,6 +11,9 @@
#include <gtk/gtk.h>
#include <functional>
#include <vector>
#ifdef MOZ_WAYLAND
# include "mozilla/Mutex.h"
#endif
/*
* MozContainer
@ -87,6 +90,10 @@ struct _MozContainer {
gboolean surface_needs_clear;
gboolean ready_to_draw;
std::vector<std::function<void(void)>> initial_draw_cbs;
// mozcontainer is used from Compositor and Rendering threads
// so we need to control access to mozcontainer where wayland internals
// are used directly.
mozilla::Mutex* container_lock;
#endif
gboolean force_default_visual;
};