Skip to content

Truly independent web browser

LibCore+LibGfx+ImageDecoder: Decode images into IOSurfaces on macOS

Aliaksandr Kalenikddaf3ff6e5f7
diff --git a/Libraries/LibCore/IOSurface.cpp b/Libraries/LibCore/IOSurface.cpp
index 12fee8a88f6..c2a8bcfdc54 100644
--- a/Libraries/LibCore/IOSurface.cpp
+++ b/Libraries/LibCore/IOSurface.cpp
@@ -98,6 +98,38 @@ IOSurfaceHandle IOSurfaceHandle::create(int width, int height)
return IOSurfaceHandle(make<IOSurfaceRefWrapper>(ref));
}
+ErrorOr<IOSurfaceHandle> IOSurfaceHandle::try_create_with_four_bytes_per_pixel(int width, int height, u32 pixel_format)
+{
+ int bytes_per_element = 4;
+
+ RefAutoRelease<CFNumberRef> width_number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &width);
+ RefAutoRelease<CFNumberRef> height_number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &height);
+ RefAutoRelease<CFNumberRef> bytes_per_element_number = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &bytes_per_element);
+ RefAutoRelease<CFNumberRef> pixel_format_number = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pixel_format);
+
+ RefAutoRelease<CFMutableDictionaryRef> props = CFDictionaryCreateMutable(kCFAllocatorDefault,
+ 0,
+ &kCFTypeDictionaryKeyCallBacks,
+ &kCFTypeDictionaryValueCallBacks);
+ if (!width_number.ref() || !height_number.ref() || !bytes_per_element_number.ref() || !pixel_format_number.ref() || !props.ref())
+ return Error::from_errno(ENOMEM);
+
+ CFDictionarySetValue(props.ref(), kIOSurfaceWidth, width_number.ref());
+ CFDictionarySetValue(props.ref(), kIOSurfaceHeight, height_number.ref());
+ CFDictionarySetValue(props.ref(), kIOSurfaceBytesPerElement, bytes_per_element_number.ref());
+ CFDictionarySetValue(props.ref(), kIOSurfacePixelFormat, pixel_format_number.ref());
+
+ auto* ref = IOSurfaceCreate(props.ref());
+ if (!ref)
+ return Error::from_string_literal("Could not create an IOSurface");
+ return IOSurfaceHandle(make<IOSurfaceRefWrapper>(ref));
+}
+
+size_t IOSurfaceHandle::bytes_per_row_aligned_as_the_system_aligns_it(size_t minimum_bytes_per_row)
+{
+ return IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, minimum_bytes_per_row);
+}
+
u32 IOSurfaceHandle::id() const
{
return IOSurfaceGetID(m_ref_wrapper->ref);
diff --git a/Libraries/LibCore/IOSurface.h b/Libraries/LibCore/IOSurface.h
index e9cd64524e5..f74ee7b322b 100644
--- a/Libraries/LibCore/IOSurface.h
+++ b/Libraries/LibCore/IOSurface.h
@@ -24,6 +24,9 @@ public:
static IOSurfaceHandle create(int width, int height);
static ErrorOr<IOSurfaceHandle> from_mach_port(MachPort const& port);
+ static ErrorOr<IOSurfaceHandle> try_create_with_four_bytes_per_pixel(int width, int height, u32 pixel_format);
+ static size_t bytes_per_row_aligned_as_the_system_aligns_it(size_t minimum_bytes_per_row);
+
// Adopts a surface owned by someone else, such as one a decoder hands back, by retaining it.
static IOSurfaceHandle from_ref(void* io_surface_ref);
diff --git a/Libraries/LibGfx/Bitmap.cpp b/Libraries/LibGfx/Bitmap.cpp
index 907022fbb18..7c4a0002a21 100644
--- a/Libraries/LibGfx/Bitmap.cpp
+++ b/Libraries/LibGfx/Bitmap.cpp
@@ -91,6 +91,86 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::create_shareable(BitmapFormat format, Alp
return bitmap;
}
+#ifdef AK_OS_MACOS
+static u32 iosurface_pixel_format_of(BitmapFormat format)
+{
+ switch (format) {
+ case BitmapFormat::BGRx8888:
+ case BitmapFormat::BGRA8888:
+ return 'BGRA';
+ case BitmapFormat::RGBx8888:
+ case BitmapFormat::RGBA8888:
+ return 'RGBA';
+ case BitmapFormat::Invalid:
+ break;
+ }
+ VERIFY_NOT_REACHED();
+}
+
+ErrorOr<void> Bitmap::validate_iosurface_holds_pixels_of(Core::IOSurfaceHandle const& iosurface, BitmapFormat format, IntSize size)
+{
+ if (!is_valid_bitmap_format(to_underlying(format)))
+ return Error::from_string_literal("Gfx::Bitmap IOSurface format is invalid");
+ if (size.is_empty() || size_would_overflow(format, size))
+ return Error::from_string_literal("Gfx::Bitmap IOSurface size is invalid");
+ if (iosurface.plane_count() != 0 || iosurface.bytes_per_element() != 4 || iosurface.pixel_format() != iosurface_pixel_format_of(format))
+ return Error::from_string_literal("Gfx::Bitmap IOSurface does not hold pixels of its format");
+ if (iosurface.width() != static_cast<size_t>(size.width()) || iosurface.height() != static_cast<size_t>(size.height()))
+ return Error::from_string_literal("Gfx::Bitmap IOSurface is not of its size");
+
+ auto bytes_per_row = iosurface.bytes_per_row();
+ if (bytes_per_row < minimum_pitch(size.width(), format) || bytes_per_row % alignof(RawPixel) != 0)
+ return Error::from_string_literal("Gfx::Bitmap IOSurface rows are invalid");
+ Checked<size_t> size_of_pixels = bytes_per_row;
+ size_of_pixels *= static_cast<size_t>(size.height());
+ if (size_of_pixels.has_overflow() || size_of_pixels.value() > iosurface.alloc_size())
+ return Error::from_string_literal("Gfx::Bitmap IOSurface rows are outside of its memory");
+ if (!iosurface.data())
+ return Error::from_string_literal("Gfx::Bitmap IOSurface is not mapped");
+ return {};
+}
+
+ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::create_with_iosurface(BitmapFormat format, AlphaType alpha_type, IntSize size, Core::IOSurfaceHandle iosurface)
+{
+ TRY(validate_iosurface_holds_pixels_of(iosurface, format, size));
+ return adopt_nonnull_ref_or_enomem(new (nothrow) Bitmap(format, alpha_type, size, move(iosurface)));
+}
+
+Bitmap::Bitmap(BitmapFormat format, AlphaType alpha_type, IntSize size, Core::IOSurfaceHandle iosurface)
+ : m_size(size)
+ , m_data(iosurface.data())
+ , m_pitch(iosurface.bytes_per_row())
+ , m_format(format)
+ , m_alpha_type(alpha_type)
+ , m_iosurface(move(iosurface))
+{
+}
+
+static ErrorOr<NonnullRefPtr<Bitmap>> create_over_new_iosurface(BitmapFormat format, AlphaType alpha_type, IntSize size)
+{
+ if (size.is_empty() || Bitmap::size_would_overflow(format, size))
+ return Error::from_string_literal("Gfx::Bitmap IOSurface size is invalid");
+
+ // NB: The rows of a surface only a few pixels wide are mostly padding, which would take more memory than shared
+ // memory and a copy of it on the GPU take together.
+ auto minimum_pitch = Bitmap::minimum_pitch(size.width(), format);
+ if (Core::IOSurfaceHandle::bytes_per_row_aligned_as_the_system_aligns_it(minimum_pitch) > 2 * minimum_pitch)
+ return Error::from_string_literal("Gfx::Bitmap IOSurface rows would be mostly padding");
+
+ auto iosurface = TRY(Core::IOSurfaceHandle::try_create_with_four_bytes_per_pixel(size.width(), size.height(), iosurface_pixel_format_of(format)));
+ return Bitmap::create_with_iosurface(format, alpha_type, size, move(iosurface));
+}
+#endif
+
+ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::create_shareable_with_gpu_access(BitmapFormat format, AlphaType alpha_type, IntSize size)
+{
+#ifdef AK_OS_MACOS
+ if (auto bitmap = create_over_new_iosurface(format, alpha_type, size); !bitmap.is_error())
+ return bitmap.release_value();
+#endif
+ return create_shareable(format, alpha_type, size);
+}
+
Bitmap::Bitmap(BitmapFormat format, AlphaType alpha_type, IntSize size, BackingStore const& backing_store)
: m_size(size)
, m_data(backing_store.data)
@@ -166,8 +246,8 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::clone() const
{
auto new_bitmap = TRY(Bitmap::create(format(), alpha_type(), size()));
- VERIFY(size_in_bytes() == new_bitmap->size_in_bytes());
- memcpy(new_bitmap->scanline(0), scanline(0), size_in_bytes());
+ for (int y = 0; y < height(); ++y)
+ memcpy(new_bitmap->scanline_u8(y), scanline_u8(y), new_bitmap->pitch());
return new_bitmap;
}
@@ -206,6 +286,19 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::to_bitmap_backed_by_anonymous_buffer() co
return bitmap;
}
+ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::to_bitmap_shareable_with_gpu_access() const
+{
+ if (is_in_shareable_memory()) {
+ // FIXME: The const_cast here is awkward.
+ return NonnullRefPtr { const_cast<Bitmap&>(*this) };
+ }
+ auto bitmap = TRY(Bitmap::create_shareable_with_gpu_access(format(), alpha_type(), size()));
+ auto const row_size = minimum_pitch(width(), format());
+ for (int y = 0; y < height(); ++y)
+ memcpy(bitmap->scanline_u8(y), scanline_u8(y), row_size);
+ return bitmap;
+}
+
Bitmap::~Bitmap()
{
if (m_destruction_callback)
diff --git a/Libraries/LibGfx/Bitmap.h b/Libraries/LibGfx/Bitmap.h
index 97a0298699b..567461d3568 100644
--- a/Libraries/LibGfx/Bitmap.h
+++ b/Libraries/LibGfx/Bitmap.h
@@ -15,6 +15,11 @@
#include <LibGfx/Rect.h>
#include <LibGfx/ScalingMode.h>
+#ifdef AK_OS_MACOS
+# include <AK/Optional.h>
+# include <LibCore/IOSurface.h>
+#endif
+
namespace Gfx {
// A pixel value that does not express any information about its component order
@@ -59,11 +64,21 @@ public:
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_with_raw_data(BitmapFormat, AlphaType, ReadonlyBytes, IntSize);
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_with_anonymous_buffer(BitmapFormat, AlphaType, Core::AnonymousBuffer, IntSize);
+ // In memory that other processes can map and the GPU can sample without a copy where the platform has such memory,
+ // which is an IOSurface on macOS. Elsewhere, or when no such memory can be had, this is create_shareable().
+ [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_shareable_with_gpu_access(BitmapFormat, AlphaType, IntSize);
+
+#ifdef AK_OS_MACOS
+ [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_with_iosurface(BitmapFormat, AlphaType, IntSize, Core::IOSurfaceHandle);
+ [[nodiscard]] static ErrorOr<void> validate_iosurface_holds_pixels_of(Core::IOSurfaceHandle const&, BitmapFormat, IntSize);
+#endif
+
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> clone() const;
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> cropped(Gfx::IntRect, Gfx::Color outside_color = Gfx::Color::Black) const;
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap_backed_by_anonymous_buffer() const;
+ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap_shareable_with_gpu_access() const;
[[nodiscard]] ShareableBitmap to_shareable_bitmap() const;
@@ -107,6 +122,19 @@ public:
[[nodiscard]] Core::AnonymousBuffer& anonymous_buffer() { return m_buffer; }
[[nodiscard]] Core::AnonymousBuffer const& anonymous_buffer() const { return m_buffer; }
+#ifdef AK_OS_MACOS
+ [[nodiscard]] Core::IOSurfaceHandle const* iosurface() const { return m_iosurface.has_value() ? &m_iosurface.value() : nullptr; }
+#endif
+
+ [[nodiscard]] bool is_in_shareable_memory() const
+ {
+#ifdef AK_OS_MACOS
+ if (m_iosurface.has_value())
+ return true;
+#endif
+ return m_buffer.is_valid();
+ }
+
struct DiffResult {
bool identical { false };
@@ -137,6 +165,9 @@ private:
Bitmap(BitmapFormat, AlphaType, IntSize, BackingStore const&);
Bitmap(BitmapFormat, AlphaType, IntSize, size_t pitch, void*, Function<void()>&& destruction_callback);
Bitmap(BitmapFormat, AlphaType, Core::AnonymousBuffer, IntSize);
+#ifdef AK_OS_MACOS
+ Bitmap(BitmapFormat, AlphaType, IntSize, Core::IOSurfaceHandle);
+#endif
enum class InitializeBackingStore {
No,
@@ -151,6 +182,9 @@ private:
AlphaType m_alpha_type { AlphaType::Premultiplied };
Core::AnonymousBuffer m_buffer;
Function<void()> m_destruction_callback;
+#ifdef AK_OS_MACOS
+ Optional<Core::IOSurfaceHandle> m_iosurface;
+#endif
};
ALWAYS_INLINE u8* Bitmap::unchecked_scanline_u8(int y)
diff --git a/Libraries/LibGfx/CMYKBitmap.cpp b/Libraries/LibGfx/CMYKBitmap.cpp
index 39298c604ba..69ba1808c42 100644
--- a/Libraries/LibGfx/CMYKBitmap.cpp
+++ b/Libraries/LibGfx/CMYKBitmap.cpp
@@ -24,7 +24,7 @@ ErrorOr<NonnullRefPtr<CMYKBitmap>> CMYKBitmap::create_with_size(IntSize const& s
ErrorOr<NonnullRefPtr<Bitmap>> CMYKBitmap::to_low_quality_rgb() const
{
if (!m_rgb_bitmap) {
- m_rgb_bitmap = TRY(Bitmap::create_shareable(BitmapFormat::BGRx8888, AlphaType::Premultiplied, m_size));
+ m_rgb_bitmap = TRY(Bitmap::create_shareable_with_gpu_access(BitmapFormat::BGRx8888, AlphaType::Premultiplied, m_size));
for (int y = 0; y < m_size.height(); ++y) {
for (int x = 0; x < m_size.width(); ++x) {
diff --git a/Libraries/LibGfx/ImageFormats/AVIFLoader.cpp b/Libraries/LibGfx/ImageFormats/AVIFLoader.cpp
index dac93c7ab37..4c9307870e7 100644
--- a/Libraries/LibGfx/ImageFormats/AVIFLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/AVIFLoader.cpp
@@ -107,7 +107,7 @@ static ErrorOr<void> decode_avif_image(AVIFLoadingContext& context)
avifRGBImage rgb;
while (avifDecoderNextImage(context.decoder) == AVIF_RESULT_OK) {
auto bitmap_format = context.has_alpha ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888;
- auto bitmap = TRY(Bitmap::create_shareable(bitmap_format, AlphaType::Unpremultiplied, context.size.value()));
+ auto bitmap = TRY(Bitmap::create_shareable_with_gpu_access(bitmap_format, AlphaType::Unpremultiplied, context.size.value()));
avifRGBImageSetDefaults(&rgb, context.decoder->image);
rgb.pixels = bitmap->scanline_u8(0);
diff --git a/Libraries/LibGfx/ImageFormats/BMPLoader.cpp b/Libraries/LibGfx/ImageFormats/BMPLoader.cpp
index 7187d57c473..4b2f116f166 100644
--- a/Libraries/LibGfx/ImageFormats/BMPLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/BMPLoader.cpp
@@ -1318,7 +1318,7 @@ static ErrorOr<void> decode_bmp_pixel_data(BMPLoadingContext& context)
u32 const absolute_height = static_cast<u32>(abs(static_cast<i64>(context.dib.core.height)));
u32 const height = !context.is_included_in_ico ? absolute_height : (absolute_height / 2);
- context.bitmap = TRY(Bitmap::create_shareable(format, Gfx::AlphaType::Unpremultiplied, { static_cast<int>(width), static_cast<int>(height) }));
+ context.bitmap = TRY(Bitmap::create_shareable_with_gpu_access(format, Gfx::AlphaType::Unpremultiplied, { static_cast<int>(width), static_cast<int>(height) }));
ByteBuffer rle_buffer;
ReadonlyBytes bytes { context.file_bytes + context.data_offset, context.file_size - context.data_offset };
diff --git a/Libraries/LibGfx/ImageFormats/ExifOrientedBitmap.h b/Libraries/LibGfx/ImageFormats/ExifOrientedBitmap.h
index 5039a7d87d9..155f53a5609 100644
--- a/Libraries/LibGfx/ImageFormats/ExifOrientedBitmap.h
+++ b/Libraries/LibGfx/ImageFormats/ExifOrientedBitmap.h
@@ -16,7 +16,7 @@ class ExifOrientedBitmap {
public:
static ErrorOr<ExifOrientedBitmap> create(TIFF::Orientation orientation, IntSize size, BitmapFormat format)
{
- auto bitmap = TRY(Bitmap::create_shareable(format, AlphaType::Premultiplied, oriented_size(size, orientation)));
+ auto bitmap = TRY(Bitmap::create_shareable_with_gpu_access(format, AlphaType::Premultiplied, oriented_size(size, orientation)));
return ExifOrientedBitmap(move(bitmap), size, orientation);
}
diff --git a/Libraries/LibGfx/ImageFormats/GIFLoader.cpp b/Libraries/LibGfx/ImageFormats/GIFLoader.cpp
index 461f80f42e0..ad02c6078bd 100644
--- a/Libraries/LibGfx/ImageFormats/GIFLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/GIFLoader.cpp
@@ -293,7 +293,7 @@ ErrorOr<ImageFrameDescriptor> GIFImageDecoderPlugin::frame(size_t index, Optiona
while (context.frames_decoded <= index)
TRY(decode_next_frame(context));
- return ImageFrameDescriptor { TRY(context.canvas->to_bitmap_backed_by_anonymous_buffer()), context.frame_durations[index] };
+ return ImageFrameDescriptor { TRY(context.canvas->to_bitmap_shareable_with_gpu_access()), context.frame_durations[index] };
}
}
diff --git a/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp b/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp
index 4ed80872ee8..d275de31c28 100644
--- a/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp
@@ -95,7 +95,7 @@ ErrorOr<void> JPEGLoadingContext::decode()
bool could_read_all_scanlines = true;
if (cinfo.out_color_space == JCS_EXT_BGRX) {
- rgb_bitmap = TRY(Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRx8888, Gfx::AlphaType::Premultiplied, { static_cast<int>(cinfo.output_width), static_cast<int>(cinfo.output_height) }));
+ rgb_bitmap = TRY(Gfx::Bitmap::create_shareable_with_gpu_access(Gfx::BitmapFormat::BGRx8888, Gfx::AlphaType::Premultiplied, { static_cast<int>(cinfo.output_width), static_cast<int>(cinfo.output_height) }));
while (cinfo.output_scanline < cinfo.output_height) {
auto* row_ptr = (u8*)rgb_bitmap->scanline(cinfo.output_scanline);
auto out_size = jpeg_read_scanlines(&cinfo, &row_ptr, 1);
diff --git a/Libraries/LibGfx/ImageFormats/JPEGXLLoader.cpp b/Libraries/LibGfx/ImageFormats/JPEGXLLoader.cpp
index b3054313ee1..77088716ad9 100644
--- a/Libraries/LibGfx/ImageFormats/JPEGXLLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/JPEGXLLoader.cpp
@@ -76,7 +76,7 @@ private:
if (self.m_pending_bitmap)
return false;
- auto bitmap_or_error = Bitmap::create_shareable(Gfx::BitmapFormat::RGBA8888, self.m_alpha_type, self.m_size);
+ auto bitmap_or_error = Bitmap::create_shareable_with_gpu_access(Gfx::BitmapFormat::RGBA8888, self.m_alpha_type, self.m_size);
if (bitmap_or_error.is_error())
return false;
diff --git a/Libraries/LibGfx/ImageFormats/PNGLoader.cpp b/Libraries/LibGfx/ImageFormats/PNGLoader.cpp
index 24809ced8b0..9af0f34adae 100644
--- a/Libraries/LibGfx/ImageFormats/PNGLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/PNGLoader.cpp
@@ -81,7 +81,7 @@ ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> PNGImageDecoderPlugin::create(Readonl
// NOTE: If we didn't fail in initialize(), that means we have size information.
// We can create a single-frame bitmap with that size and return it.
// This is weird, but kinda matches the behavior of other browsers.
- auto bitmap = TRY(Bitmap::create_shareable(BitmapFormat::BGRA8888, AlphaType::Premultiplied, decoder->m_context->size));
+ auto bitmap = TRY(Bitmap::create_shareable_with_gpu_access(BitmapFormat::BGRA8888, AlphaType::Premultiplied, decoder->m_context->size));
decoder->m_context->frame_descriptors.append({ move(bitmap), 0 });
decoder->m_context->frame_count = 1;
return decoder;
@@ -344,7 +344,7 @@ ErrorOr<size_t> PNGLoadingContext::read_frames(png_structp png_ptr, png_infop in
// Allocate into the in_flight_bitmap member — so the bitmap survives a potential libpng longjmp out of
// png_read_image() below.
if (use == DecodedFrameUse::Output)
- in_flight_bitmap = TRY(Bitmap::create_shareable(BitmapFormat::BGRA8888, AlphaType::Unpremultiplied, frame_size));
+ in_flight_bitmap = TRY(Bitmap::create_shareable_with_gpu_access(BitmapFormat::BGRA8888, AlphaType::Unpremultiplied, frame_size));
else
in_flight_bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, AlphaType::Unpremultiplied, frame_size));
@@ -418,7 +418,7 @@ ErrorOr<size_t> PNGLoadingContext::read_frames(png_structp png_ptr, png_infop in
}
animation_frame_count++;
- frame_descriptors.append({ TRY(output_buffer->to_bitmap_backed_by_anonymous_buffer()), duration_ms() });
+ frame_descriptors.append({ TRY(output_buffer->to_bitmap_shareable_with_gpu_access()), duration_ms() });
switch (dispose_op) {
case PNG_DISPOSE_OP_NONE:
diff --git a/Libraries/LibGfx/ImageFormats/WebPLoader.cpp b/Libraries/LibGfx/ImageFormats/WebPLoader.cpp
index 78931a208fe..60cea3db4d3 100644
--- a/Libraries/LibGfx/ImageFormats/WebPLoader.cpp
+++ b/Libraries/LibGfx/ImageFormats/WebPLoader.cpp
@@ -170,8 +170,10 @@ static ErrorOr<ImageFrameDescriptor> decode_next_webp_animation_frame(WebPLoadin
return Error::from_string_literal("Failed to decode animated frame");
auto bitmap_format = context.has_alpha ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888;
- auto bitmap = TRY(Bitmap::create_shareable(bitmap_format, Gfx::AlphaType::Unpremultiplied, context.size));
- memcpy(bitmap->scanline_u8(0), frame_data, context.size.width() * context.size.height() * 4);
+ auto bitmap = TRY(Bitmap::create_shareable_with_gpu_access(bitmap_format, Gfx::AlphaType::Unpremultiplied, context.size));
+ auto const frame_row_size = Bitmap::minimum_pitch(context.size.width(), bitmap_format);
+ for (int y = 0; y < context.size.height(); ++y)
+ memcpy(bitmap->scanline_u8(y), frame_data + y * frame_row_size, frame_row_size);
auto duration = timestamp - context.anim_old_timestamp;
context.anim_old_timestamp = timestamp;
@@ -186,7 +188,7 @@ static ErrorOr<void> decode_webp_image(WebPLoadingContext& context)
VERIFY(!context.has_animation);
auto bitmap_format = context.has_alpha ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888;
- auto bitmap = TRY(Bitmap::create_shareable(bitmap_format, Gfx::AlphaType::Unpremultiplied, context.size));
+ auto bitmap = TRY(Bitmap::create_shareable_with_gpu_access(bitmap_format, Gfx::AlphaType::Unpremultiplied, context.size));
auto image_data = WebPDecodeBGRAInto(context.data.data(), context.data.size(), bitmap->scanline_u8(0), bitmap->data_size(), bitmap->pitch());
if (image_data == nullptr)
diff --git a/Services/ImageDecoder/SandboxMacOS.cpp b/Services/ImageDecoder/SandboxMacOS.cpp
index 9afb2318f11..78e77b730f9 100644
--- a/Services/ImageDecoder/SandboxMacOS.cpp
+++ b/Services/ImageDecoder/SandboxMacOS.cpp
@@ -5,6 +5,7 @@
*/
#include <ImageDecoder/Sandbox.h>
+#include <LibCore/System.h>
#include <LibSandbox/Sandbox.h>
namespace ImageDecoder {
@@ -13,7 +14,20 @@ ErrorOr<void> apply_sandbox(StringView mach_server_name)
{
TRY(Sandbox::configure_runtime());
- return Sandbox::apply_macos_sandbox({ .mach_server_name = mach_server_name });
+ // Images are decoded into IOSurfaces, which the GPU samples where they are. CoreFoundation looks up the main bundle
+ // when IOSurface sets itself up, so the decoder reads its own executable and application bundle.
+ auto executable_path = TRY(Core::System::current_executable_path());
+
+ Vector<Sandbox::SeatbeltPath> paths;
+ TRY(Sandbox::add_seatbelt_path_if_exists(paths, executable_path, Sandbox::SeatbeltPath::Access::ReadOnly));
+ if (auto bundle = Sandbox::application_bundle_for_executable(executable_path); bundle.has_value())
+ TRY(Sandbox::add_seatbelt_path_if_exists(paths, *bundle, Sandbox::SeatbeltPath::Access::ReadOnly));
+
+ return Sandbox::apply_macos_sandbox({
+ .paths = paths.span(),
+ .mach_server_name = mach_server_name,
+ .system_services = Sandbox::SystemService::IOSurface,
+ });
}
}
diff --git a/Tests/LibGfx/TestImageDecoder.cpp b/Tests/LibGfx/TestImageDecoder.cpp
index 2edf10dfd96..05b9c6e4c57 100644
--- a/Tests/LibGfx/TestImageDecoder.cpp
+++ b/Tests/LibGfx/TestImageDecoder.cpp
@@ -112,7 +112,7 @@ TEST_CASE(test_frames_are_decoded_into_shareable_bitmaps)
continue;
for (size_t index = 0; index < decoder->frame_count(); ++index) {
auto frame = TRY_OR_FAIL(decoder->frame(index));
- if (!frame.image->anonymous_buffer().is_valid())
+ if (!frame.image->is_in_shareable_memory())
FAIL(ByteString::formatted("Frame {} of {} is not shareable", index, path));
}
}