Skip to content

Truly independent web browser

LibCore+LibGfx: Let shared image buffers report a failure to allocate

Aliaksandr Kalenik11162c584448
diff --git a/Libraries/LibCore/IOSurface.cpp b/Libraries/LibCore/IOSurface.cpp
index b4a1ab663ef..bdcec0542f1 100644
--- a/Libraries/LibCore/IOSurface.cpp
+++ b/Libraries/LibCore/IOSurface.cpp
@@ -79,6 +79,11 @@ static CFPropertyListRef srgb_color_space_property_list()
}
IOSurfaceHandle IOSurfaceHandle::create(int width, int height)
+{
+ return MUST(try_create(width, height));
+}
+
+ErrorOr<IOSurfaceHandle> IOSurfaceHandle::try_create(int width, int height)
{
size_t bytes_per_element = 4;
uint32_t pixel_format = 'BGRA';
@@ -99,7 +104,8 @@ IOSurfaceHandle IOSurfaceHandle::create(int width, int height)
CFDictionarySetValue(props.ref(), kIOSurfacePixelFormat, pixel_format_number.ref());
auto* ref = IOSurfaceCreate(props.ref());
- VERIFY(ref);
+ if (!ref)
+ return Error::from_string_literal("Unable to create an IOSurface");
IOSurfaceSetValue(ref, CFSTR("IOSurfaceColorSpace"), srgb_color_space_property_list());
return IOSurfaceHandle(make<IOSurfaceRefWrapper>(ref));
}
diff --git a/Libraries/LibCore/IOSurface.h b/Libraries/LibCore/IOSurface.h
index e8cf031d241..b0096e9793b 100644
--- a/Libraries/LibCore/IOSurface.h
+++ b/Libraries/LibCore/IOSurface.h
@@ -24,6 +24,7 @@ public:
IOSurfaceHandle& operator=(IOSurfaceHandle&& other);
static IOSurfaceHandle create(int width, int height);
+ static ErrorOr<IOSurfaceHandle> try_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);
diff --git a/Libraries/LibGfx/SharedImageBuffer.cpp b/Libraries/LibGfx/SharedImageBuffer.cpp
index 66a5a1ab9e7..70a57e11068 100644
--- a/Libraries/LibGfx/SharedImageBuffer.cpp
+++ b/Libraries/LibGfx/SharedImageBuffer.cpp
@@ -76,13 +76,18 @@ SharedImageBuffer::SharedImageBuffer(WindowsD3DHandle&& d3d_handle)
#endif
SharedImageBuffer SharedImageBuffer::create(IntSize size)
+{
+ return MUST(try_create(size));
+}
+
+ErrorOr<SharedImageBuffer> SharedImageBuffer::try_create(IntSize size)
{
#ifdef AK_OS_MACOS
- auto iosurface_handle = Core::IOSurfaceHandle::create(size.width(), size.height());
+ auto iosurface_handle = TRY(Core::IOSurfaceHandle::try_create(size.width(), size.height()));
auto bitmap = create_bitmap_from_iosurface(iosurface_handle);
return SharedImageBuffer(move(iosurface_handle), move(bitmap));
#else
- return SharedImageBuffer(MUST(Bitmap::create_shareable(shared_image_buffer_format, shared_image_buffer_alpha_type, size)));
+ return SharedImageBuffer(TRY(Bitmap::create_shareable(shared_image_buffer_format, shared_image_buffer_alpha_type, size)));
#endif
}
diff --git a/Libraries/LibGfx/SharedImageBuffer.h b/Libraries/LibGfx/SharedImageBuffer.h
index 154409a38cd..835de8b95e0 100644
--- a/Libraries/LibGfx/SharedImageBuffer.h
+++ b/Libraries/LibGfx/SharedImageBuffer.h
@@ -25,6 +25,7 @@ public:
AK_ALLOC_WITH_KMALLOC;
static SharedImageBuffer create(IntSize);
+ static ErrorOr<SharedImageBuffer> try_create(IntSize);
static SharedImageBuffer import_from_shared_image(SharedImage);
SharedImageBuffer(SharedImageBuffer&&);