diff --git a/widget/gtk/WaylandSurfaceLock.h b/widget/gtk/WaylandSurfaceLock.h new file mode 100644 --- /dev/null +++ b/widget/gtk/WaylandSurfaceLock.h @@ -0,0 +1,39 @@ +/* -*- 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/. */ + +#ifndef __MOZ_WAYLAND_SURFACE_LOCK_H__ +#define __MOZ_WAYLAND_SURFACE_LOCK_H__ + +struct wl_surface; +struct _MozContainer; +typedef struct _MozContainer MozContainer; + +namespace mozilla::widget { + +class WaylandSurface; + +// WaylandSurfaceLock is used to change and commit to wl_surface in atomic way +// and prevents failures if explicit sync is used (Bug 1898476). +// Also it's used as an argument if we need to make sure WaylandSurface is +// locked. +class WaylandSurfaceLock final { + public: + explicit WaylandSurfaceLock(RefPtr aWaylandSurface); + ~WaylandSurfaceLock(); + + WaylandSurface* GetWaylandSurface(); + + private: +#ifdef MOZ_WAYLAND + RefPtr mWaylandSurface; + wl_surface* mSurface = nullptr; +#endif +}; + +} // namespace mozilla::widget + +#endif /* __MOZ_WAYLAND_SURFACE_LOCK_H__ */ diff --git a/widget/gtk/WaylandSurfaceLock.cpp b/widget/gtk/WaylandSurfaceLock.cpp new file mode 100644 --- /dev/null +++ b/widget/gtk/WaylandSurfaceLock.cpp @@ -0,0 +1,47 @@ +/* -*- 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/. */ + +#include "WaylandSurfaceLock.h" +#ifdef MOZ_WAYLAND +# include "WaylandSurface.h" +#endif +#include "MozContainer.h" +#include "WidgetUtilsGtk.h" + +namespace mozilla::widget { + +WaylandSurfaceLock::WaylandSurfaceLock(RefPtr aWaylandSurface) { +#ifdef MOZ_WAYLAND + mWaylandSurface = std::move(aWaylandSurface); + if (GdkIsWaylandDisplay()) { + MOZ_DIAGNOSTIC_ASSERT(mWaylandSurface); + // mSurface can be nullptr if we lock hidden MozContainer and + // that's correct, MozContainer is still locked. + mSurface = mWaylandSurface->Lock(this); + } +#endif +} + +WaylandSurfaceLock::~WaylandSurfaceLock() { +#ifdef MOZ_WAYLAND + if (GdkIsWaylandDisplay()) { + mWaylandSurface->Commit(this, /* force commit */ false, + /* flush display */ false); + mWaylandSurface->Unlock(&mSurface, this); + } +#endif +} + +WaylandSurface* WaylandSurfaceLock::GetWaylandSurface() { +#ifdef MOZ_WAYLAND + return mWaylandSurface.get(); +#else + return nullptr; +#endif +} + +} // namespace mozilla::widget