Skip to content

Commit

Permalink
Merge mozilla-central to autoland a=merge on a CLOSED TREE
Browse files Browse the repository at this point in the history
  • Loading branch information
chorotan committed Apr 24, 2023
2 parents cab6cd3 + 91e7aee commit 78b8d8e
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 189 deletions.
2 changes: 1 addition & 1 deletion gfx/gl/SharedSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ UniquePtr<SurfaceFactory> SurfaceFactory::Create(
case layers::TextureType::DMABUF:
#ifdef MOZ_WAYLAND
if (gl.GetContextType() == GLContextType::EGL &&
widget::nsDMABufDevice::IsDMABufWebGLEnabled()) {
widget::GetDMABufDevice()->IsDMABufWebGLEnabled()) {
return SurfaceFactory_DMABUF::Create(gl);
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions gfx/gl/SharedSurfaceDMABUF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Maybe<layers::SurfaceDescriptor> SharedSurface_DMABUF::ToSurfaceDescriptor() {

/*static*/
UniquePtr<SurfaceFactory_DMABUF> SurfaceFactory_DMABUF::Create(GLContext& gl) {
if (!widget::nsDMABufDevice::IsDMABufWebGLEnabled()) {
if (!widget::GetDMABufDevice()->IsDMABufWebGLEnabled()) {
return nullptr;
}

Expand All @@ -100,7 +100,7 @@ UniquePtr<SurfaceFactory_DMABUF> SurfaceFactory_DMABUF::Create(GLContext& gl) {

LOGDMABUF(
("SurfaceFactory_DMABUF::Create() failed, fallback to SW buffers.\n"));
widget::nsDMABufDevice::DisableDMABufWebGL();
widget::GetDMABufDevice()->DisableDMABufWebGL();
return nullptr;
}

Expand Down
2 changes: 1 addition & 1 deletion gfx/layers/CanvasRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ TextureType TexTypeForWebgl(KnowsCompositor* const knowsCompositor) {
#ifdef MOZ_WAYLAND
if (kIsWayland) {
if (!knowsCompositor->UsingSoftwareWebRender() &&
widget::nsDMABufDevice::IsDMABufWebGLEnabled()) {
widget::GetDMABufDevice()->IsDMABufWebGLEnabled()) {
return TextureType::DMABUF;
}
}
Expand Down
2 changes: 1 addition & 1 deletion gfx/layers/NativeLayerWayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ NativeLayerWayland::NativeLayerWayland(
MOZ_RELEASE_ASSERT(mSurfacePoolHandle,
"Need a non-null surface pool handle.");

widget::nsWaylandDisplay* waylandDisplay = widget::WaylandDisplayGet();
RefPtr<widget::nsWaylandDisplay> waylandDisplay = widget::WaylandDisplayGet();
wl_compositor* compositor = waylandDisplay->GetCompositor();
mWlSurface = wl_compositor_create_surface(compositor);

Expand Down
2 changes: 1 addition & 1 deletion gfx/layers/client/TextureClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ static TextureType GetTextureType(gfx::SurfaceFormat aFormat,
#ifdef MOZ_WAYLAND
if ((layersBackend == LayersBackend::LAYERS_WR &&
!aKnowsCompositor->UsingSoftwareWebRender()) &&
widget::nsDMABufDevice::IsDMABufTexturesEnabled() &&
widget::GetDMABufDevice()->IsDMABufTexturesEnabled() &&
aFormat != SurfaceFormat::A8) {
return TextureType::DMABUF;
}
Expand Down
2 changes: 1 addition & 1 deletion gfx/thebes/gfxPlatformGtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void gfxPlatformGtk::InitDmabufConfig() {
gfxInfo->GetDrmRenderDevice(drmRenderDevice);
gfxVars::SetDrmRenderDevice(drmRenderDevice);

if (GetDMABufDevice()->IsEnabled(failureId)) {
if (!GetDMABufDevice()->Configure(failureId)) {
feature.ForceDisable(FeatureStatus::Failed, "Failed to configure",
failureId);
}
Expand Down
9 changes: 0 additions & 9 deletions toolkit/xre/nsAppRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5112,15 +5112,6 @@ int XREMain::XRE_mainStartup(bool* aExitFlag) {
GfxInfo::FireGLXTestProcess();
#endif

#ifdef MOZ_WAYLAND
// Make sure we have wayland connection for main thread.
// It's used as template to create display connections
// for different threads.
if (IsWaylandEnabled()) {
MOZ_UNUSED(WaylandDisplayGet());
}
#endif

return 0;
}

Expand Down
189 changes: 144 additions & 45 deletions widget/gtk/DMABufLibWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ using namespace mozilla::gfx;
namespace mozilla {
namespace widget {

bool nsDMABufDevice::sUseWebGLDmabufBackend = true;

#define GBMLIB_NAME "libgbm.so.1"
#define DRMLIB_NAME "libdrm.so.2"

Expand Down Expand Up @@ -130,40 +128,74 @@ bool nsGbmLib::Load() {
}

gbm_device* nsDMABufDevice::GetGbmDevice() { return mGbmDevice; }
int nsDMABufDevice::GetDRMFd() { return mDRMFd; }

bool nsDMABufDevice::IsEnabled(nsACString& aFailureId) {
if (mDRMFd == -1) {
aFailureId = mFailureId;
static void dmabuf_modifiers(void* data,
struct zwp_linux_dmabuf_v1* zwp_linux_dmabuf,
uint32_t format, uint32_t modifier_hi,
uint32_t modifier_lo) {
// skip modifiers marked as invalid
if (modifier_hi == (DRM_FORMAT_MOD_INVALID >> 32) &&
modifier_lo == (DRM_FORMAT_MOD_INVALID & 0xffffffff)) {
return;
}
return mDRMFd != -1;
}

nsDMABufDevice::nsDMABufDevice() { Configure(); }

nsDMABufDevice::~nsDMABufDevice() {
if (mGbmDevice) {
nsGbmLib::DestroyDevice(mGbmDevice);
mGbmDevice = nullptr;
}
if (mDRMFd != -1) {
close(mDRMFd);
mDRMFd = -1;
auto* device = static_cast<nsDMABufDevice*>(data);
switch (format) {
case GBM_FORMAT_ARGB8888:
device->AddFormatModifier(true, format, modifier_hi, modifier_lo);
break;
case GBM_FORMAT_XRGB8888:
device->AddFormatModifier(false, format, modifier_hi, modifier_lo);
break;
default:
break;
}
}

void nsDMABufDevice::Configure() {
if (mInitialized) {
return;
}
mInitialized = true;
static void dmabuf_format(void* data,
struct zwp_linux_dmabuf_v1* zwp_linux_dmabuf,
uint32_t format) {
// XXX: deprecated
}

LOGDMABUF(("nsDMABufDevice::Configure()"));
static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
dmabuf_format, dmabuf_modifiers};

static void global_registry_handler(void* data, wl_registry* registry,
uint32_t id, const char* interface,
uint32_t version) {
auto* device = static_cast<nsDMABufDevice*>(data);
if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0 && version > 2) {
auto* dmabuf = WaylandRegistryBind<zwp_linux_dmabuf_v1>(
registry, id, &zwp_linux_dmabuf_v1_interface, 3);
LOGDMABUF(("zwp_linux_dmabuf_v1 is available."));
device->ResetFormatsModifiers();
zwp_linux_dmabuf_v1_add_listener(dmabuf, &dmabuf_listener, data);
} else if (strcmp(interface, "wl_drm") == 0) {
LOGDMABUF(("wl_drm is available."));
}
}

if (!nsGbmLib::IsAvailable()) {
LOGDMABUF(("nsGbmLib is not available!"));
mFailureId = "FEATURE_FAILURE_NO_LIBGBM";
return;
static void global_registry_remover(void* data, wl_registry* registry,
uint32_t id) {}

static const struct wl_registry_listener registry_listener = {
global_registry_handler, global_registry_remover};

nsDMABufDevice::nsDMABufDevice()
: mUseWebGLDmabufBackend(true),
mXRGBFormat({true, false, GBM_FORMAT_XRGB8888, nullptr, 0}),
mARGBFormat({true, true, GBM_FORMAT_ARGB8888, nullptr, 0}),
mDRMFd(-1),
mGbmDevice(nullptr),
mInitialized(false) {
if (GdkIsWaylandDisplay()) {
wl_display* display = WaylandDisplayGetWLDisplay();
wl_registry* registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, &registry_listener, this);
wl_display_roundtrip(display);
wl_display_roundtrip(display);
wl_registry_destroy(registry);
}

nsAutoCString drm_render_node(getenv("MOZ_DRM_DEVICE"));
Expand All @@ -177,30 +209,50 @@ void nsDMABufDevice::Configure() {
if (mDRMFd < 0) {
LOGDMABUF(("Failed to open drm render node %s error %s\n",
drm_render_node.get(), strerror(errno)));
mFailureId = "FEATURE_FAILURE_NO_DRM_DEVICE";
return;
}
} else {
LOGDMABUF(("We're missing DRM render device!\n"));
mFailureId = "FEATURE_FAILURE_NO_DRM_DEVICE";
return;
}
}

nsDMABufDevice::~nsDMABufDevice() {
if (mGbmDevice) {
nsGbmLib::DestroyDevice(mGbmDevice);
mGbmDevice = nullptr;
}
if (mDRMFd != -1) {
close(mDRMFd);
mDRMFd = -1;
}
}

int nsDMABufDevice::GetDRMFd() { return mDRMFd; }

bool nsDMABufDevice::Configure(nsACString& aFailureId) {
if (mInitialized) {
return true;
}

LOGDMABUF(("nsDMABufDevice::Configure()"));
mInitialized = true;

// mGbmDevice is optional and it's used to create dmabuf surfaces
// directly on GPU. Some drivers (NVIDIA) doesn't support that
// but we still can use mDRMFd to operate with dmabuf surfaces
// created by GFX drivers and exported by OpenGL.
if (!nsGbmLib::IsAvailable()) {
LOGDMABUF(("nsGbmLib is not available!"));
aFailureId = "FEATURE_FAILURE_NO_LIBGBM";
return false;
}

// fd passed to gbm_create_device() should be kept open until
// gbm_device_destroy() is called.
mGbmDevice = nsGbmLib::CreateDevice(mDRMFd);
mGbmDevice = nsGbmLib::CreateDevice(GetDRMFd());
if (!mGbmDevice) {
LOGDMABUF(
("Failed to create drm render device. Direct dmabuf surface creation "
"will be disabled."));
LOGDMABUF(("Failed to create drm render device"));
aFailureId = "FEATURE_FAILURE_BAD_DRM_RENDER_NODE";
return false;
}

LOGDMABUF(("DMABuf is enabled"));
return true;
}

#ifdef NIGHTLY_BUILD
Expand All @@ -214,21 +266,68 @@ bool nsDMABufDevice::IsDMABufTexturesEnabled() { return false; }
bool nsDMABufDevice::IsDMABufWebGLEnabled() {
LOGDMABUF(
("nsDMABufDevice::IsDMABufWebGLEnabled: UseDMABuf %d "
"sUseWebGLDmabufBackend %d "
"mUseWebGLDmabufBackend %d "
"widget_dmabuf_webgl_enabled %d\n",
gfx::gfxVars::UseDMABuf(), sUseWebGLDmabufBackend,
gfx::gfxVars::UseDMABuf(), mUseWebGLDmabufBackend,
StaticPrefs::widget_dmabuf_webgl_enabled()));
return gfx::gfxVars::UseDMABuf() && sUseWebGLDmabufBackend &&
return gfx::gfxVars::UseDMABuf() && mUseWebGLDmabufBackend &&
StaticPrefs::widget_dmabuf_webgl_enabled();
}

void nsDMABufDevice::DisableDMABufWebGL() { sUseWebGLDmabufBackend = false; }
void nsDMABufDevice::DisableDMABufWebGL() { mUseWebGLDmabufBackend = false; }

GbmFormat* nsDMABufDevice::GetGbmFormat(bool aHasAlpha) {
GbmFormat* format = aHasAlpha ? &mARGBFormat : &mXRGBFormat;
return format->mIsSupported ? format : nullptr;
}

GbmFormat* nsDMABufDevice::GetExactGbmFormat(int aFormat) {
if (aFormat == mARGBFormat.mFormat) {
return &mARGBFormat;
} else if (aFormat == mXRGBFormat.mFormat) {
return &mXRGBFormat;
}

return nullptr;
}

void nsDMABufDevice::AddFormatModifier(bool aHasAlpha, int aFormat,
uint32_t mModifierHi,
uint32_t mModifierLo) {
GbmFormat* format = aHasAlpha ? &mARGBFormat : &mXRGBFormat;
format->mIsSupported = true;
format->mHasAlpha = aHasAlpha;
format->mFormat = aFormat;
format->mModifiersCount++;
format->mModifiers =
(uint64_t*)realloc(format->mModifiers,
format->mModifiersCount * sizeof(*format->mModifiers));
format->mModifiers[format->mModifiersCount - 1] =
((uint64_t)mModifierHi << 32) | mModifierLo;
}

void nsDMABufDevice::ResetFormatsModifiers() {
mARGBFormat.mModifiersCount = 0;
free(mARGBFormat.mModifiers);
mARGBFormat.mModifiers = nullptr;

mXRGBFormat.mModifiersCount = 0;
free(mXRGBFormat.mModifiers);
mXRGBFormat.mModifiers = nullptr;
}

// TODO: Make private or make sure it's configured
nsDMABufDevice* GetDMABufDevice() {
static nsDMABufDevice dmaBufDevice;
return &dmaBufDevice;
}

nsDMABufDevice* GetAndConfigureDMABufDevice() {
nsCString failureId;
if (GetDMABufDevice()->Configure(failureId)) {
return GetDMABufDevice();
}
return nullptr;
}

} // namespace widget
} // namespace mozilla
39 changes: 27 additions & 12 deletions widget/gtk/DMABufLibWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,34 +173,49 @@ class nsGbmLib {
static mozilla::StaticMutex sDRILock MOZ_UNANNOTATED;
};

struct GbmFormat {
bool mIsSupported;
bool mHasAlpha;
int mFormat;
uint64_t* mModifiers;
int mModifiersCount;
};

class nsDMABufDevice {
public:
nsDMABufDevice();
~nsDMABufDevice();

int GetDRMFd();
gbm_device* GetGbmDevice();

bool IsEnabled(nsACString& aFailureId);

// Use dmabuf for WebRender general web content
static bool IsDMABufTexturesEnabled();
bool IsDMABufTexturesEnabled();
// Use dmabuf for WebGL content
static bool IsDMABufWebGLEnabled();
static void DisableDMABufWebGL();
bool IsDMABufWebGLEnabled();
void DisableDMABufWebGL();

int GetDRMFd();
GbmFormat* GetGbmFormat(bool aHasAlpha);
GbmFormat* GetExactGbmFormat(int aFormat);
void ResetFormatsModifiers();
void AddFormatModifier(bool aHasAlpha, int aFormat, uint32_t mModifierHi,
uint32_t mModifierLo);
bool Configure(nsACString& aFailureId);

private:
void Configure();
bool mUseWebGLDmabufBackend;

int mDRMFd = -1;
gbm_device* mGbmDevice = nullptr;
bool mInitialized = false;
const char* mFailureId = nullptr;
private:
GbmFormat mXRGBFormat;
GbmFormat mARGBFormat;

static bool sUseWebGLDmabufBackend;
int mDRMFd;
gbm_device* mGbmDevice;
bool mInitialized;
};

nsDMABufDevice* GetDMABufDevice();
nsDMABufDevice* GetAndConfigureDMABufDevice();

} // namespace widget
} // namespace mozilla
Expand Down
Loading

0 comments on commit 78b8d8e

Please sign in to comment.