From 4350bccf8e8c2c6151bc665d3553236eab626c29 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 20 Aug 2025 22:01:30 +0200 Subject: [PATCH] LibWeb: Use Accelerate framework on macOS to premultiply bitmap data This leverages hardware acceleration to speed things up considerably, shaving ~500ms of load time off of https://cloudflare.com/ --- Libraries/LibGfx/Bitmap.cpp | 41 +++++++++++++++++++++++++++++++-- Libraries/LibGfx/CMakeLists.txt | 1 + 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/Libraries/LibGfx/Bitmap.cpp b/Libraries/LibGfx/Bitmap.cpp index 5434ba8b1fe..8f0a2a4aea6 100644 --- a/Libraries/LibGfx/Bitmap.cpp +++ b/Libraries/LibGfx/Bitmap.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2024, Andreas Kling + * Copyright (c) 2018-2025, Andreas Kling * Copyright (c) 2022, Timothy Slater * Copyright (c) 2024, Jelle Raaijmakers * @@ -12,6 +12,10 @@ #include #include +#ifdef AK_OS_MACOS +# include +#endif + namespace Gfx { struct BackingStore { @@ -280,6 +284,39 @@ void Bitmap::set_alpha_type_destructive(AlphaType alpha_type) if (alpha_type == m_alpha_type) return; +#ifdef AK_OS_MACOS + vImage_Buffer buf { .data = m_data, .height = vImagePixelCount(height()), .width = vImagePixelCount(width()), .rowBytes = pitch() }; + vImage_Error err; + if (m_alpha_type == AlphaType::Unpremultiplied) { + switch (m_format) { + case BitmapFormat::BGRA8888: + case BitmapFormat::BGRx8888: + err = vImagePremultiplyData_BGRA8888(&buf, &buf, kvImageNoFlags); + break; + case BitmapFormat::RGBA8888: + case BitmapFormat::RGBx8888: + err = vImagePremultiplyData_RGBA8888(&buf, &buf, kvImageNoFlags); + break; + default: + VERIFY_NOT_REACHED(); + } + } else { + switch (m_format) { + case BitmapFormat::BGRA8888: + case BitmapFormat::BGRx8888: + err = vImageUnpremultiplyData_BGRA8888(&buf, &buf, kvImageNoFlags); + break; + case BitmapFormat::RGBA8888: + case BitmapFormat::RGBx8888: + err = vImageUnpremultiplyData_RGBA8888(&buf, &buf, kvImageNoFlags); + break; + default: + VERIFY_NOT_REACHED(); + } + } + VERIFY(err == kvImageNoError); +#else + // FIXME: Make this fast on other platforms too. if (m_alpha_type == AlphaType::Unpremultiplied) { for (auto y = 0; y < height(); ++y) { for (auto x = 0; x < width(); ++x) @@ -293,7 +330,7 @@ void Bitmap::set_alpha_type_destructive(AlphaType alpha_type) } else { VERIFY_NOT_REACHED(); } - +#endif m_alpha_type = alpha_type; } diff --git a/Libraries/LibGfx/CMakeLists.txt b/Libraries/LibGfx/CMakeLists.txt index 7868393080d..59e702a1cba 100644 --- a/Libraries/LibGfx/CMakeLists.txt +++ b/Libraries/LibGfx/CMakeLists.txt @@ -168,6 +168,7 @@ endif() if (APPLE) target_link_libraries(LibCore PUBLIC "-framework Metal") + target_link_libraries(LibCore PUBLIC "-framework Accelerate") endif() if (HAS_VULKAN)