Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Compositor/BackingStoreFactory.h>
#include <Compositor/ConnectionFromClient.h>
#include <Compositor/FontSkia.h>
#include <Compositor/MainDispatchQueue.h>
#include <Compositor/Sandbox.h>
#include <Compositor/SkiaBackendContext.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/CrashHandler.h>
#include <LibCore/EventLoop.h>
#include <LibCore/Platform/TaskRole.h>
#include <LibCore/Platform/ThreadQoS.h>
#include <LibCore/Process.h>
#include <LibCore/ResourceImplementationFile.h>
#include <LibCore/StatsLog.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/GpuDeviceIdentity.h>
#include <LibIPC/SingleServer.h>
#include <LibMain/Main.h>
#include <LibPlatformFeatures/ProcessFeatures.h>
ErrorOr<int> ladybird_main(Main::Arguments arguments)
{
AK::set_rich_debug_enabled(true);
StringView mach_server_name;
StringView cache_path;
StringView resource_root;
bool wait_for_debugger = false;
bool enable_test_mode = false;
StringView platform_features;
bool force_fontconfig = false;
bool disable_sandbox = false;
StringView gpu_device;
int crash_report_fd = -1;
StringView stats_log_directory;
Core::ArgsParser args_parser;
args_parser.add_option(stats_log_directory, "Directory for process statistics (JSON lines)", "stats-log-dir", 0, "path");
args_parser.add_option(crash_report_fd, "Descriptor for anonymous crash diagnostics", "crash-report-fd", 0, "fd");
args_parser.add_option(mach_server_name, "Mach server name", "mach-server-name", 0, "mach_server_name");
args_parser.add_option(cache_path, "Path to the profile cache", "cache-path", 0, "path");
args_parser.add_option(resource_root, "Path to the browser's resources", "resource-root", 0, "path");
args_parser.add_option(wait_for_debugger, "Wait for debugger", "wait-for-debugger");
args_parser.add_option(enable_test_mode, "Enable test mode", "test-mode");
args_parser.add_option(platform_features, "The platform features the Browser decided this process may use", "platform-features", 0, "feature,...");
args_parser.add_option(force_fontconfig, "Force using fontconfig for font loading", "force-fontconfig");
args_parser.add_option(disable_sandbox, "Disable process sandboxing", "disable-sandbox");
args_parser.add_option(gpu_device, "The GPU to paint on, as the presentation identifies it", "gpu-device", 0, "identity");
StringView drm_format_modifiers;
args_parser.add_option(drm_format_modifiers, "The DRM format modifiers the presentation can import from that GPU", "drm-format-modifiers", 0, "hex,...");
args_parser.parse(arguments);
auto stats_log = TRY(Core::StatsLog::create(stats_log_directory, "Compositor"sv));
if (resource_root.is_empty())
return Error::from_string_literal("--resource-root is required");
PlatformFeatures::ProcessFeatures::initialize_for_current_process(TRY(PlatformFeatures::ProcessFeatures::from_string(PlatformFeatures::FeatureProcess::Compositor, platform_features)));
auto const& features = PlatformFeatures::ProcessFeatures::the();
Optional<Gfx::GpuDeviceIdentity> preferred_gpu_device;
if (!gpu_device.is_empty()) {
preferred_gpu_device = Gfx::GpuDeviceIdentity::from_string(gpu_device);
if (!preferred_gpu_device.has_value())
return Error::from_string_literal("Malformed GPU device identity");
}
if (!drm_format_modifiers.is_empty()) {
Vector<u64> modifiers;
for (auto modifier : drm_format_modifiers.split_view(',')) {
auto value = modifier.to_number<u64>(TrimWhitespace::No, 16);
if (!value.has_value())
return Error::from_string_literal("Malformed DRM format modifier");
modifiers.append(*value);
}
Compositor::set_presentation_drm_format_modifiers(move(modifiers));
}
if (crash_report_fd >= 0) {
if (auto result = Core::CrashHandler::initialize(crash_report_fd); result.is_error())
warnln("Could not install crash report handler: {}", result.error());
}
if (wait_for_debugger)
Core::Process::wait_for_debugger_and_break();
if (auto result = Core::Platform::adopt_foreground_application_task_role(); result.is_error())
warnln("Could not adopt the foreground application task role: {}", result.error());
if (auto result = Core::Platform::set_current_thread_qos(Core::Platform::ThreadQoS::UserInteractive); result.is_error())
warnln("Could not set main thread QoS: {}", result.error());
if (enable_test_mode)
Compositor::use_test_font_rendering();
Core::ResourceImplementation::install(make<Core::ResourceImplementationFile>(TRY(String::from_utf8(resource_root))));
if (force_fontconfig)
Compositor::set_font_rasterizer(Compositor::FontRasterizer::FreeType);
#if defined(AK_OS_LINUX)
// NB: The GPU driver starts threads, and Landlock does not confine a thread that already runs. Seccomp covers every
// thread, so the rest of the sandbox can wait until the driver is done with the syscalls that only its setup uses.
if (!disable_sandbox)
TRY(Compositor::restrict_filesystem(resource_root));
#endif
if (features.is_enabled(PlatformFeatures::Feature::GpuPainting))
Gfx::SkiaBackendContext::initialize_gpu_backend(preferred_gpu_device);
auto skia_backend_context = Gfx::SkiaBackendContext::the_main_thread_context();
auto& event_loop = Core::EventLoop::initialize_for_current_thread();
#ifdef AK_OS_MACOS
if (auto result = Compositor::drain_main_dispatch_queue_in_event_loop(); result.is_error())
warnln("Unable to drain the main dispatch queue, so HDR video will not show: {}", result.error());
#endif
if (!disable_sandbox)
TRY(Compositor::apply_sandbox(mach_server_name, cache_path, resource_root));
Optional<Gfx::GpuDeviceIdentity> gpu_device_identity;
#ifdef USE_VULKAN_DMABUF_IMAGES
// Hardware decoding only starts once the GPU it decodes on is named, which is only done when its frames can be imported.
if (skia_backend_context && features.is_enabled(PlatformFeatures::Feature::YCbCrImport) && skia_backend_context->vulkan_context().can_sample_through_ycbcr_conversion)
gpu_device_identity = skia_backend_context->vulkan_context().device_identity;
#endif
auto client = TRY(IPC::take_over_accepted_client_from_system_server<Compositor::ConnectionFromClient>(
mach_server_name, move(skia_backend_context), enable_test_mode));
client->async_gpu_device_identity_reported(move(gpu_device_identity));
if (stats_log)
stats_log->start_sampling([](JsonObject& values) {
if (auto context = Gfx::SkiaBackendContext::the_main_thread_context())
values.set("gpu.skia_resource_cache_bytes"sv, context->resource_cache_bytes());
});
return event_loop.exec();
}