mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:57:35 +00:00
LibGfx: Use ErrorOr<T> for Gfx::Bitmap::allocate_backing_store()
This commit is contained in:
parent
56992f90b7
commit
af562c857e
2 changed files with 9 additions and 11 deletions
|
@ -67,10 +67,10 @@ static bool size_would_overflow(BitmapFormat format, const IntSize& size, int sc
|
||||||
|
|
||||||
RefPtr<Bitmap> Bitmap::try_create(BitmapFormat format, const IntSize& size, int scale_factor)
|
RefPtr<Bitmap> Bitmap::try_create(BitmapFormat format, const IntSize& size, int scale_factor)
|
||||||
{
|
{
|
||||||
auto backing_store = Bitmap::try_allocate_backing_store(format, size, scale_factor);
|
auto backing_store_or_error = Bitmap::allocate_backing_store(format, size, scale_factor);
|
||||||
if (!backing_store.has_value())
|
if (backing_store_or_error.is_error())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
return adopt_ref(*new Bitmap(format, size, scale_factor, backing_store.value()));
|
return adopt_ref(*new Bitmap(format, size, scale_factor, backing_store_or_error.release_value()));
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<Bitmap> Bitmap::try_create_shareable(BitmapFormat format, const IntSize& size, int scale_factor)
|
RefPtr<Bitmap> Bitmap::try_create_shareable(BitmapFormat format, const IntSize& size, int scale_factor)
|
||||||
|
@ -607,10 +607,10 @@ ShareableBitmap Bitmap::to_shareable_bitmap() const
|
||||||
return ShareableBitmap(*bitmap);
|
return ShareableBitmap(*bitmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<BackingStore> Bitmap::try_allocate_backing_store(BitmapFormat format, IntSize const& size, int scale_factor)
|
ErrorOr<BackingStore> Bitmap::allocate_backing_store(BitmapFormat format, IntSize const& size, int scale_factor)
|
||||||
{
|
{
|
||||||
if (size_would_overflow(format, size, scale_factor))
|
if (size_would_overflow(format, size, scale_factor))
|
||||||
return {};
|
return AK::Error::from_string_literal("Gfx::Bitmap backing store size overflow"sv);
|
||||||
|
|
||||||
const auto pitch = minimum_pitch(size.width() * scale_factor, format);
|
const auto pitch = minimum_pitch(size.width() * scale_factor, format);
|
||||||
const auto data_size_in_bytes = size_in_bytes(pitch, size.height() * scale_factor);
|
const auto data_size_in_bytes = size_in_bytes(pitch, size.height() * scale_factor);
|
||||||
|
@ -622,11 +622,9 @@ Optional<BackingStore> Bitmap::try_allocate_backing_store(BitmapFormat format, I
|
||||||
#else
|
#else
|
||||||
void* data = mmap(nullptr, data_size_in_bytes, PROT_READ | PROT_WRITE, map_flags, 0, 0);
|
void* data = mmap(nullptr, data_size_in_bytes, PROT_READ | PROT_WRITE, map_flags, 0, 0);
|
||||||
#endif
|
#endif
|
||||||
if (data == MAP_FAILED) {
|
if (data == MAP_FAILED)
|
||||||
perror("mmap");
|
return AK::Error::from_errno(errno);
|
||||||
return {};
|
return BackingStore { data, pitch, data_size_in_bytes };
|
||||||
}
|
|
||||||
return { { data, pitch, data_size_in_bytes } };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bitmap::allocate_palette_from_format(BitmapFormat format, const Vector<RGBA32>& source_palette)
|
void Bitmap::allocate_palette_from_format(BitmapFormat format, const Vector<RGBA32>& source_palette)
|
||||||
|
|
|
@ -241,7 +241,7 @@ private:
|
||||||
Bitmap(BitmapFormat, const IntSize&, int, size_t pitch, void*);
|
Bitmap(BitmapFormat, const IntSize&, int, size_t pitch, void*);
|
||||||
Bitmap(BitmapFormat, Core::AnonymousBuffer, const IntSize&, int, const Vector<RGBA32>& palette);
|
Bitmap(BitmapFormat, Core::AnonymousBuffer, const IntSize&, int, const Vector<RGBA32>& palette);
|
||||||
|
|
||||||
static Optional<BackingStore> try_allocate_backing_store(BitmapFormat format, IntSize const& size, int scale_factor);
|
static ErrorOr<BackingStore> allocate_backing_store(BitmapFormat format, IntSize const& size, int scale_factor);
|
||||||
|
|
||||||
void allocate_palette_from_format(BitmapFormat, const Vector<RGBA32>& source_palette);
|
void allocate_palette_from_format(BitmapFormat, const Vector<RGBA32>& source_palette);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue