ladybird/Userland/Libraries/LibGfx/ShareableBitmap.cpp
Timothy Flynn 9b483625e6 LibIPC+Everywhere: Change IPC decoders to construct values in-place
Currently, the generated IPC decoders will default-construct the type to
be decoded, then pass that value by reference to the concrete decoder.
This, of course, requires that the type is default-constructible. This
was an issue for decoding Variants, which had to require the first type
in the Variant list is Empty, to ensure it is default constructible.

Further, this made it possible for values to become uninitialized in
user-defined decoders.

This patch makes the decoder interface such that the concrete decoders
themselves contruct the decoded type upon return from the decoder. To do
so, the default decoders in IPC::Decoder had to be moved to the IPC
namespace scope, as these decoders are now specializations instead of
overloaded methods (C++ requires specializations to be in a namespace
scope).
2022-12-26 09:36:16 +01:00

68 lines
2.1 KiB
C++

/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/Bitmap.h>
#include <LibGfx/ShareableBitmap.h>
#include <LibGfx/Size.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
#include <LibIPC/File.h>
namespace Gfx {
ShareableBitmap::ShareableBitmap(NonnullRefPtr<Bitmap> bitmap, Tag)
: m_bitmap(move(bitmap))
{
}
}
namespace IPC {
template<>
bool encode(Encoder& encoder, Gfx::ShareableBitmap const& shareable_bitmap)
{
encoder << shareable_bitmap.is_valid();
if (!shareable_bitmap.is_valid())
return true;
auto& bitmap = *shareable_bitmap.bitmap();
encoder << IPC::File(bitmap.anonymous_buffer().fd());
encoder << bitmap.size();
encoder << static_cast<u32>(bitmap.scale());
encoder << static_cast<u32>(bitmap.format());
if (bitmap.is_indexed()) {
auto palette = bitmap.palette_to_vector();
encoder << palette;
}
return true;
}
template<>
ErrorOr<Gfx::ShareableBitmap> decode(Decoder& decoder)
{
if (auto valid = TRY(decoder.decode<bool>()); !valid)
return Gfx::ShareableBitmap {};
auto anon_file = TRY(decoder.decode<IPC::File>());
auto size = TRY(decoder.decode<Gfx::IntSize>());
auto scale = TRY(decoder.decode<u32>());
auto raw_bitmap_format = TRY(decoder.decode<u32>());
if (!Gfx::is_valid_bitmap_format(raw_bitmap_format))
return Error::from_string_literal("IPC: Invalid Gfx::ShareableBitmap format");
auto bitmap_format = static_cast<Gfx::BitmapFormat>(raw_bitmap_format);
Vector<Gfx::ARGB32> palette;
if (Gfx::Bitmap::is_indexed(bitmap_format))
palette = TRY(decoder.decode<decltype(palette)>());
auto buffer = TRY(Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(size.width() * scale, bitmap_format), size.height() * scale)));
auto bitmap = TRY(Gfx::Bitmap::try_create_with_anonymous_buffer(bitmap_format, move(buffer), size, scale, palette));
return Gfx::ShareableBitmap { move(bitmap), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };
}
}