1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:27:35 +00:00

LibIPC+IPCCompiler+AK: Make IPC value decoders return ErrorOr<void>

This allows us to use TRY() in decoding helpers, leading to a nice
reduction in line count.
This commit is contained in:
Andreas Kling 2021-11-28 11:56:31 +01:00
parent 8d76eb773f
commit cb9cac4e40
21 changed files with 207 additions and 296 deletions

View file

@ -39,43 +39,33 @@ bool encode(Encoder& encoder, const Gfx::ShareableBitmap& shareable_bitmap)
return true;
}
bool decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
ErrorOr<void> decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
{
bool valid = false;
if (!decoder.decode(valid))
return false;
TRY(decoder.decode(valid));
if (!valid) {
shareable_bitmap = {};
return true;
return {};
}
IPC::File anon_file;
if (!decoder.decode(anon_file))
return false;
TRY(decoder.decode(anon_file));
Gfx::IntSize size;
if (!decoder.decode(size))
return false;
TRY(decoder.decode(size));
u32 scale;
if (!decoder.decode(scale))
return false;
TRY(decoder.decode(scale));
u32 raw_bitmap_format;
if (!decoder.decode(raw_bitmap_format))
return false;
TRY(decoder.decode(raw_bitmap_format));
if (!Gfx::is_valid_bitmap_format(raw_bitmap_format))
return false;
return Error::from_string_literal("IPC: Invalid Gfx::ShareableBitmap format"sv);
auto bitmap_format = (Gfx::BitmapFormat)raw_bitmap_format;
Vector<Gfx::RGBA32> palette;
if (Gfx::Bitmap::is_indexed(bitmap_format)) {
if (!decoder.decode(palette))
return false;
TRY(decoder.decode(palette));
}
auto buffer_or_error = 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_or_error.is_error())
return false;
auto bitmap_or_error = Gfx::Bitmap::try_create_with_anonymous_buffer(bitmap_format, buffer_or_error.release_value(), size, scale, palette);
if (bitmap_or_error.is_error())
return false;
shareable_bitmap = Gfx::ShareableBitmap { bitmap_or_error.release_value(), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };
return true;
auto buffer = TRY(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())));
auto bitmap = TRY(Gfx::Bitmap::try_create_with_anonymous_buffer(bitmap_format, move(buffer), size, scale, palette));
shareable_bitmap = Gfx::ShareableBitmap { move(bitmap), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };
return {};
}
}