2020-04-06 11:09:01 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-06 11:09:01 +02:00
|
|
|
*/
|
|
|
|
|
2020-03-29 19:04:05 +02:00
|
|
|
#include <LibGfx/Bitmap.h>
|
|
|
|
#include <LibGfx/ShareableBitmap.h>
|
2020-07-25 21:31:47 -07:00
|
|
|
#include <LibGfx/Size.h>
|
2020-03-29 19:04:05 +02:00
|
|
|
#include <LibIPC/Decoder.h>
|
2020-05-12 18:52:51 +02:00
|
|
|
#include <LibIPC/Encoder.h>
|
2021-01-15 22:36:36 +01:00
|
|
|
#include <LibIPC/File.h>
|
2020-03-29 19:04:05 +02:00
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
2021-01-16 11:23:22 +01:00
|
|
|
ShareableBitmap::ShareableBitmap(NonnullRefPtr<Bitmap> bitmap, Tag)
|
|
|
|
: m_bitmap(move(bitmap))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:04:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2022-11-15 11:24:59 -05:00
|
|
|
template<>
|
2023-01-01 23:37:35 -05:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, Gfx::ShareableBitmap const& shareable_bitmap)
|
2020-05-12 18:52:51 +02:00
|
|
|
{
|
2023-01-01 23:37:35 -05:00
|
|
|
TRY(encoder.encode(shareable_bitmap.is_valid()));
|
2021-01-16 10:58:02 +01:00
|
|
|
if (!shareable_bitmap.is_valid())
|
2023-01-01 23:37:35 -05:00
|
|
|
return {};
|
|
|
|
|
2021-01-16 23:57:57 +01:00
|
|
|
auto& bitmap = *shareable_bitmap.bitmap();
|
2023-01-01 23:37:35 -05:00
|
|
|
TRY(encoder.encode(IPC::File(bitmap.anonymous_buffer().fd())));
|
|
|
|
TRY(encoder.encode(bitmap.size()));
|
|
|
|
TRY(encoder.encode(static_cast<u32>(bitmap.scale())));
|
|
|
|
TRY(encoder.encode(static_cast<u32>(bitmap.format())));
|
|
|
|
return {};
|
2020-05-12 18:52:51 +02:00
|
|
|
}
|
|
|
|
|
2022-11-15 11:24:59 -05:00
|
|
|
template<>
|
2022-12-22 20:40:33 -05:00
|
|
|
ErrorOr<Gfx::ShareableBitmap> decode(Decoder& decoder)
|
2020-03-29 19:04:05 +02:00
|
|
|
{
|
2022-12-22 20:40:33 -05:00
|
|
|
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>());
|
2021-01-16 23:57:57 +01:00
|
|
|
if (!Gfx::is_valid_bitmap_format(raw_bitmap_format))
|
2022-07-11 17:57:32 +00:00
|
|
|
return Error::from_string_literal("IPC: Invalid Gfx::ShareableBitmap format");
|
2022-12-22 20:40:33 -05:00
|
|
|
|
|
|
|
auto bitmap_format = static_cast<Gfx::BitmapFormat>(raw_bitmap_format);
|
|
|
|
|
2022-12-06 00:04:41 +01:00
|
|
|
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)));
|
2023-10-11 12:35:50 +02:00
|
|
|
auto bitmap = TRY(Gfx::Bitmap::create_with_anonymous_buffer(bitmap_format, move(buffer), size, scale));
|
2022-12-22 20:40:33 -05:00
|
|
|
|
|
|
|
return Gfx::ShareableBitmap { move(bitmap), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };
|
2020-03-29 19:04:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|