mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 08:17:34 +00:00
LibGfx: Use "try_" prefix for static factory functions
Also mark them as [[nodiscard]].
This commit is contained in:
parent
f0409081f5
commit
c7d891765c
131 changed files with 422 additions and 421 deletions
|
@ -1185,7 +1185,7 @@ static bool decode_bmp_pixel_data(BMPLoadingContext& context)
|
|||
|
||||
const u32 width = abs(context.dib.core.width);
|
||||
const u32 height = abs(context.dib.core.height);
|
||||
context.bitmap = Bitmap::create_purgeable(format, { static_cast<int>(width), static_cast<int>(height) });
|
||||
context.bitmap = Bitmap::try_create_purgeable(format, { static_cast<int>(width), static_cast<int>(height) });
|
||||
if (!context.bitmap) {
|
||||
dbgln("BMP appears to have overly large dimensions");
|
||||
return false;
|
||||
|
|
|
@ -65,7 +65,7 @@ static bool size_would_overflow(BitmapFormat format, const IntSize& size, int sc
|
|||
return Checked<size_t>::multiplication_would_overflow(pitch, size.height() * scale_factor);
|
||||
}
|
||||
|
||||
RefPtr<Bitmap> Bitmap::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::allocate_backing_store(format, size, scale_factor, Purgeable::No);
|
||||
if (!backing_store.has_value())
|
||||
|
@ -73,7 +73,7 @@ RefPtr<Bitmap> Bitmap::create(BitmapFormat format, const IntSize& size, int scal
|
|||
return adopt_ref(*new Bitmap(format, size, scale_factor, Purgeable::No, backing_store.value()));
|
||||
}
|
||||
|
||||
RefPtr<Bitmap> Bitmap::create_purgeable(BitmapFormat format, const IntSize& size, int scale_factor)
|
||||
RefPtr<Bitmap> Bitmap::try_create_purgeable(BitmapFormat format, const IntSize& size, int scale_factor)
|
||||
{
|
||||
auto backing_store = Bitmap::allocate_backing_store(format, size, scale_factor, Purgeable::Yes);
|
||||
if (!backing_store.has_value())
|
||||
|
@ -81,7 +81,7 @@ RefPtr<Bitmap> Bitmap::create_purgeable(BitmapFormat format, const IntSize& size
|
|||
return adopt_ref(*new Bitmap(format, size, scale_factor, Purgeable::Yes, backing_store.value()));
|
||||
}
|
||||
|
||||
RefPtr<Bitmap> Bitmap::create_shareable(BitmapFormat format, const IntSize& size, int scale_factor)
|
||||
RefPtr<Bitmap> Bitmap::try_create_shareable(BitmapFormat format, const IntSize& size, int scale_factor)
|
||||
{
|
||||
if (size_would_overflow(format, size, scale_factor))
|
||||
return nullptr;
|
||||
|
@ -92,7 +92,7 @@ RefPtr<Bitmap> Bitmap::create_shareable(BitmapFormat format, const IntSize& size
|
|||
auto buffer = Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(data_size, PAGE_SIZE));
|
||||
if (!buffer.is_valid())
|
||||
return nullptr;
|
||||
return Bitmap::create_with_anonymous_buffer(format, buffer, size, scale_factor, {});
|
||||
return Bitmap::try_create_with_anonymous_buffer(format, buffer, size, scale_factor, {});
|
||||
}
|
||||
|
||||
Bitmap::Bitmap(BitmapFormat format, const IntSize& size, int scale_factor, Purgeable purgeable, const BackingStore& backing_store)
|
||||
|
@ -111,14 +111,14 @@ Bitmap::Bitmap(BitmapFormat format, const IntSize& size, int scale_factor, Purge
|
|||
m_needs_munmap = true;
|
||||
}
|
||||
|
||||
RefPtr<Bitmap> Bitmap::create_wrapper(BitmapFormat format, const IntSize& size, int scale_factor, size_t pitch, void* data)
|
||||
RefPtr<Bitmap> Bitmap::try_create_wrapper(BitmapFormat format, const IntSize& size, int scale_factor, size_t pitch, void* data)
|
||||
{
|
||||
if (size_would_overflow(format, size, scale_factor))
|
||||
return nullptr;
|
||||
return adopt_ref(*new Bitmap(format, size, scale_factor, pitch, data));
|
||||
}
|
||||
|
||||
RefPtr<Bitmap> Bitmap::load_from_file(String const& path, int scale_factor)
|
||||
RefPtr<Bitmap> Bitmap::try_load_from_file(String const& path, int scale_factor)
|
||||
{
|
||||
if (scale_factor > 1 && path.starts_with("/res/")) {
|
||||
LexicalPath lexical_path { path };
|
||||
|
@ -188,7 +188,7 @@ static bool check_size(const IntSize& size, int scale_factor, BitmapFormat forma
|
|||
return true;
|
||||
}
|
||||
|
||||
RefPtr<Bitmap> Bitmap::create_with_anonymous_buffer(BitmapFormat format, Core::AnonymousBuffer buffer, const IntSize& size, int scale_factor, const Vector<RGBA32>& palette)
|
||||
RefPtr<Bitmap> Bitmap::try_create_with_anonymous_buffer(BitmapFormat format, Core::AnonymousBuffer buffer, const IntSize& size, int scale_factor, const Vector<RGBA32>& palette)
|
||||
{
|
||||
if (size_would_overflow(format, size, scale_factor))
|
||||
return nullptr;
|
||||
|
@ -205,7 +205,7 @@ RefPtr<Bitmap> Bitmap::create_with_anonymous_buffer(BitmapFormat format, Core::A
|
|||
/// - palette count
|
||||
/// - palette data (= palette count * BGRA8888)
|
||||
/// - image data (= actual size * u8)
|
||||
RefPtr<Bitmap> Bitmap::create_from_serialized_byte_buffer(ByteBuffer&& buffer)
|
||||
RefPtr<Bitmap> Bitmap::try_create_from_serialized_byte_buffer(ByteBuffer&& buffer)
|
||||
{
|
||||
InputMemoryStream stream { buffer };
|
||||
unsigned actual_size;
|
||||
|
@ -242,7 +242,7 @@ RefPtr<Bitmap> Bitmap::create_from_serialized_byte_buffer(ByteBuffer&& buffer)
|
|||
|
||||
auto data = stream.bytes().slice(stream.offset(), actual_size);
|
||||
|
||||
auto bitmap = Bitmap::create(format, { width, height }, scale_factor);
|
||||
auto bitmap = Bitmap::try_create(format, { width, height }, scale_factor);
|
||||
if (!bitmap)
|
||||
return {};
|
||||
|
||||
|
@ -303,9 +303,9 @@ RefPtr<Gfx::Bitmap> Bitmap::clone() const
|
|||
{
|
||||
RefPtr<Gfx::Bitmap> new_bitmap {};
|
||||
if (m_purgeable) {
|
||||
new_bitmap = Bitmap::create_purgeable(format(), size(), scale());
|
||||
new_bitmap = Bitmap::try_create_purgeable(format(), size(), scale());
|
||||
} else {
|
||||
new_bitmap = Bitmap::create(format(), size(), scale());
|
||||
new_bitmap = Bitmap::try_create(format(), size(), scale());
|
||||
}
|
||||
|
||||
if (!new_bitmap) {
|
||||
|
@ -320,7 +320,7 @@ RefPtr<Gfx::Bitmap> Bitmap::clone() const
|
|||
|
||||
RefPtr<Gfx::Bitmap> Bitmap::rotated(Gfx::RotationDirection rotation_direction) const
|
||||
{
|
||||
auto new_bitmap = Gfx::Bitmap::create(this->format(), { height(), width() }, scale());
|
||||
auto new_bitmap = Gfx::Bitmap::try_create(this->format(), { height(), width() }, scale());
|
||||
if (!new_bitmap)
|
||||
return nullptr;
|
||||
|
||||
|
@ -343,7 +343,7 @@ RefPtr<Gfx::Bitmap> Bitmap::rotated(Gfx::RotationDirection rotation_direction) c
|
|||
|
||||
RefPtr<Gfx::Bitmap> Bitmap::flipped(Gfx::Orientation orientation) const
|
||||
{
|
||||
auto new_bitmap = Gfx::Bitmap::create(this->format(), { width(), height() }, scale());
|
||||
auto new_bitmap = Gfx::Bitmap::try_create(this->format(), { width(), height() }, scale());
|
||||
if (!new_bitmap)
|
||||
return nullptr;
|
||||
|
||||
|
@ -368,7 +368,7 @@ RefPtr<Gfx::Bitmap> Bitmap::scaled(int sx, int sy) const
|
|||
if (sx == 1 && sy == 1)
|
||||
return this;
|
||||
|
||||
auto new_bitmap = Gfx::Bitmap::create(format(), { width() * sx, height() * sy }, scale());
|
||||
auto new_bitmap = Gfx::Bitmap::try_create(format(), { width() * sx, height() * sy }, scale());
|
||||
if (!new_bitmap)
|
||||
return nullptr;
|
||||
|
||||
|
@ -402,7 +402,7 @@ RefPtr<Gfx::Bitmap> Bitmap::scaled(float sx, float sy) const
|
|||
int scaled_width = (int)ceilf(sx * (float)width());
|
||||
int scaled_height = (int)ceilf(sy * (float)height());
|
||||
|
||||
auto new_bitmap = Gfx::Bitmap::create(format(), { scaled_width, scaled_height }, scale());
|
||||
auto new_bitmap = Gfx::Bitmap::try_create(format(), { scaled_width, scaled_height }, scale());
|
||||
if (!new_bitmap)
|
||||
return nullptr;
|
||||
|
||||
|
@ -476,7 +476,7 @@ RefPtr<Gfx::Bitmap> Bitmap::scaled(float sx, float sy) const
|
|||
|
||||
RefPtr<Gfx::Bitmap> Bitmap::cropped(Gfx::IntRect crop) const
|
||||
{
|
||||
auto new_bitmap = Gfx::Bitmap::create(format(), { crop.width(), crop.height() }, 1);
|
||||
auto new_bitmap = Gfx::Bitmap::try_create(format(), { crop.width(), crop.height() }, 1);
|
||||
if (!new_bitmap)
|
||||
return nullptr;
|
||||
|
||||
|
@ -501,7 +501,7 @@ RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_anonymous_buffer() const
|
|||
auto buffer = Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(size_in_bytes(), PAGE_SIZE));
|
||||
if (!buffer.is_valid())
|
||||
return nullptr;
|
||||
auto bitmap = Bitmap::create_with_anonymous_buffer(m_format, move(buffer), size(), scale(), palette_to_vector());
|
||||
auto bitmap = Bitmap::try_create_with_anonymous_buffer(m_format, move(buffer), size(), scale(), palette_to_vector());
|
||||
if (!bitmap)
|
||||
return nullptr;
|
||||
memcpy(bitmap->scanline(0), scanline(0), size_in_bytes());
|
||||
|
|
|
@ -90,13 +90,14 @@ enum RotationDirection {
|
|||
|
||||
class Bitmap : public RefCounted<Bitmap> {
|
||||
public:
|
||||
static RefPtr<Bitmap> create(BitmapFormat, const IntSize&, int intrinsic_scale = 1);
|
||||
static RefPtr<Bitmap> create_shareable(BitmapFormat, const IntSize&, int intrinsic_scale = 1);
|
||||
static RefPtr<Bitmap> create_purgeable(BitmapFormat, const IntSize&, int intrinsic_scale = 1);
|
||||
static RefPtr<Bitmap> create_wrapper(BitmapFormat, const IntSize&, int intrinsic_scale, size_t pitch, void*);
|
||||
static RefPtr<Bitmap> load_from_file(String const& path, int scale_factor = 1);
|
||||
static RefPtr<Bitmap> create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, const IntSize&, int intrinsic_scale, const Vector<RGBA32>& palette);
|
||||
static RefPtr<Bitmap> create_from_serialized_byte_buffer(ByteBuffer&& buffer);
|
||||
[[nodiscard]] static RefPtr<Bitmap> try_create(BitmapFormat, const IntSize&, int intrinsic_scale = 1);
|
||||
[[nodiscard]] static RefPtr<Bitmap> try_create_shareable(BitmapFormat, const IntSize&, int intrinsic_scale = 1);
|
||||
[[nodiscard]] static RefPtr<Bitmap> try_create_purgeable(BitmapFormat, const IntSize&, int intrinsic_scale = 1);
|
||||
[[nodiscard]] static RefPtr<Bitmap> try_create_wrapper(BitmapFormat, const IntSize&, int intrinsic_scale, size_t pitch, void*);
|
||||
[[nodiscard]] static RefPtr<Bitmap> try_load_from_file(String const& path, int scale_factor = 1);
|
||||
[[nodiscard]] static RefPtr<Bitmap> try_create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, const IntSize&, int intrinsic_scale, const Vector<RGBA32>& palette);
|
||||
[[nodiscard]] static RefPtr<Bitmap> try_create_from_serialized_byte_buffer(ByteBuffer&& buffer);
|
||||
|
||||
static bool is_path_a_supported_image_format(const StringView& path)
|
||||
{
|
||||
#define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
|
||||
|
|
|
@ -344,10 +344,10 @@ static const Gfx::Bitmap& circle_bitmap(bool checked, bool changing)
|
|||
void ClassicStylePainter::paint_radio_button(Painter& painter, const IntRect& rect, const Palette&, bool is_checked, bool is_being_pressed)
|
||||
{
|
||||
if (!s_unfilled_circle_bitmap) {
|
||||
s_unfilled_circle_bitmap = Bitmap::load_from_file("/res/icons/serenity/unfilled-radio-circle.png");
|
||||
s_filled_circle_bitmap = Bitmap::load_from_file("/res/icons/serenity/filled-radio-circle.png");
|
||||
s_changing_filled_circle_bitmap = Bitmap::load_from_file("/res/icons/serenity/changing-filled-radio-circle.png");
|
||||
s_changing_unfilled_circle_bitmap = Bitmap::load_from_file("/res/icons/serenity/changing-unfilled-radio-circle.png");
|
||||
s_unfilled_circle_bitmap = Bitmap::try_load_from_file("/res/icons/serenity/unfilled-radio-circle.png");
|
||||
s_filled_circle_bitmap = Bitmap::try_load_from_file("/res/icons/serenity/filled-radio-circle.png");
|
||||
s_changing_filled_circle_bitmap = Bitmap::try_load_from_file("/res/icons/serenity/changing-filled-radio-circle.png");
|
||||
s_changing_unfilled_circle_bitmap = Bitmap::try_load_from_file("/res/icons/serenity/changing-unfilled-radio-circle.png");
|
||||
}
|
||||
|
||||
auto& bitmap = circle_bitmap(is_checked, is_being_pressed);
|
||||
|
|
|
@ -792,7 +792,7 @@ static bool decode_dds(DDSLoadingContext& context)
|
|||
dbgln_if(DDS_DEBUG, "There are {} bytes remaining, we need {} for mipmap level {} of the image", stream.remaining(), needed_bytes, mipmap_level);
|
||||
VERIFY(stream.remaining() >= needed_bytes);
|
||||
|
||||
context.bitmap = Bitmap::create_purgeable(BitmapFormat::BGRA8888, { width, height });
|
||||
context.bitmap = Bitmap::try_create_purgeable(BitmapFormat::BGRA8888, { width, height });
|
||||
|
||||
decode_bitmap(stream, context, format, width, height);
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ const Bitmap* Emoji::emoji_for_code_point(u32 code_point)
|
|||
if (it != s_emojis.end())
|
||||
return (*it).value.ptr();
|
||||
|
||||
auto bitmap = Bitmap::load_from_file(String::formatted("/res/emoji/U+{:X}.png", code_point));
|
||||
auto bitmap = Bitmap::try_load_from_file(String::formatted("/res/emoji/U+{:X}.png", code_point));
|
||||
if (!bitmap) {
|
||||
s_emojis.set(code_point, nullptr);
|
||||
return nullptr;
|
||||
|
|
|
@ -94,7 +94,7 @@ public:
|
|||
if (&target == &source && (!apply_cache.m_target || !apply_cache.m_target->size().contains(source_rect.size()))) {
|
||||
// TODO: We probably don't need the entire source_rect, we could inflate
|
||||
// the target_rect appropriately
|
||||
apply_cache.m_target = Gfx::Bitmap::create(source.format(), source_rect.size());
|
||||
apply_cache.m_target = Gfx::Bitmap::try_create(source.format(), source_rect.size());
|
||||
target_rect.translate_by(-target_rect.location());
|
||||
}
|
||||
|
||||
|
|
|
@ -299,10 +299,10 @@ static bool decode_frame(GIFLoadingContext& context, size_t frame_index)
|
|||
size_t start_frame = context.current_frame + 1;
|
||||
if (context.state < GIFLoadingContext::State::FrameComplete) {
|
||||
start_frame = 0;
|
||||
context.frame_buffer = Bitmap::create_purgeable(BitmapFormat::BGRA8888, { context.logical_screen.width, context.logical_screen.height });
|
||||
context.frame_buffer = Bitmap::try_create_purgeable(BitmapFormat::BGRA8888, { context.logical_screen.width, context.logical_screen.height });
|
||||
if (!context.frame_buffer)
|
||||
return false;
|
||||
context.prev_frame_buffer = Bitmap::create_purgeable(BitmapFormat::BGRA8888, { context.logical_screen.width, context.logical_screen.height });
|
||||
context.prev_frame_buffer = Bitmap::try_create_purgeable(BitmapFormat::BGRA8888, { context.logical_screen.width, context.logical_screen.height });
|
||||
if (!context.prev_frame_buffer)
|
||||
return false;
|
||||
} else if (frame_index < context.current_frame) {
|
||||
|
|
|
@ -246,7 +246,7 @@ static bool load_ico_bmp(ICOLoadingContext& context, ICOImageDescriptor& desc)
|
|||
return false;
|
||||
}
|
||||
|
||||
desc.bitmap = Bitmap::create_purgeable(BitmapFormat::BGRA8888, { desc.width, desc.height });
|
||||
desc.bitmap = Bitmap::try_create_purgeable(BitmapFormat::BGRA8888, { desc.width, desc.height });
|
||||
if (!desc.bitmap)
|
||||
return false;
|
||||
Bitmap& bitmap = *desc.bitmap;
|
||||
|
|
|
@ -1064,7 +1064,7 @@ static void ycbcr_to_rgb(const JPGLoadingContext& context, Vector<Macroblock>& m
|
|||
|
||||
static bool compose_bitmap(JPGLoadingContext& context, const Vector<Macroblock>& macroblocks)
|
||||
{
|
||||
context.bitmap = Bitmap::create_purgeable(BitmapFormat::BGRx8888, { context.frame.width, context.frame.height });
|
||||
context.bitmap = Bitmap::try_create_purgeable(BitmapFormat::BGRx8888, { context.frame.width, context.frame.height });
|
||||
if (!context.bitmap)
|
||||
return false;
|
||||
|
||||
|
|
|
@ -606,7 +606,7 @@ static bool decode_png_bitmap_simple(PNGLoadingContext& context)
|
|||
}
|
||||
}
|
||||
|
||||
context.bitmap = Bitmap::create_purgeable(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height });
|
||||
context.bitmap = Bitmap::try_create_purgeable(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height });
|
||||
|
||||
if (!context.bitmap) {
|
||||
context.state = PNGLoadingContext::State::Error;
|
||||
|
@ -708,7 +708,7 @@ static bool decode_adam7_pass(PNGLoadingContext& context, Streamer& streamer, in
|
|||
}
|
||||
}
|
||||
|
||||
subimage_context.bitmap = Bitmap::create(context.bitmap->format(), { subimage_context.width, subimage_context.height });
|
||||
subimage_context.bitmap = Bitmap::try_create(context.bitmap->format(), { subimage_context.width, subimage_context.height });
|
||||
if (!unfilter(subimage_context)) {
|
||||
subimage_context.bitmap = nullptr;
|
||||
return false;
|
||||
|
@ -726,7 +726,7 @@ static bool decode_adam7_pass(PNGLoadingContext& context, Streamer& streamer, in
|
|||
static bool decode_png_adam7(PNGLoadingContext& context)
|
||||
{
|
||||
Streamer streamer(context.decompression_buffer->data(), context.decompression_buffer->size());
|
||||
context.bitmap = Bitmap::create_purgeable(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height });
|
||||
context.bitmap = Bitmap::try_create_purgeable(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height });
|
||||
if (!context.bitmap)
|
||||
return false;
|
||||
|
||||
|
|
|
@ -178,7 +178,7 @@ static bool read_max_val(TContext& context, Streamer& streamer)
|
|||
template<typename TContext>
|
||||
static bool create_bitmap(TContext& context)
|
||||
{
|
||||
context.bitmap = Bitmap::create_purgeable(BitmapFormat::BGRx8888, { context.width, context.height });
|
||||
context.bitmap = Bitmap::try_create_purgeable(BitmapFormat::BGRx8888, { context.width, context.height });
|
||||
if (!context.bitmap) {
|
||||
context.state = TContext::State::Error;
|
||||
return false;
|
||||
|
|
|
@ -76,7 +76,7 @@ bool decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
|
|||
auto buffer = Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(size.width(), bitmap_format), size.height()));
|
||||
if (!buffer.is_valid())
|
||||
return false;
|
||||
auto bitmap = Gfx::Bitmap::create_with_anonymous_buffer(bitmap_format, buffer, size, scale, palette);
|
||||
auto bitmap = Gfx::Bitmap::try_create_with_anonymous_buffer(bitmap_format, buffer, size, scale, palette);
|
||||
if (!bitmap)
|
||||
return false;
|
||||
shareable_bitmap = Gfx::ShareableBitmap { bitmap.release_nonnull(), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue