1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-24 00:55:06 +00:00
serenity/Libraries/LibGfx/ShareableBitmap.cpp
Andreas Kling 7cfe712f4d LibGfx+LibIPC: Add Gfx::ShareableBitmap, a bitmap for easy IPC usage
With this patch, it's now possible to pass a Gfx::ShareableBitmap in an
IPC message. As long as the message itself is synchronous, the bitmap
will be adopted by the receiving end, and disowned by the sender nicely
without any accounting effort like we've had to do in the past.

Use this in NotificationServer to allow sending arbitrary bitmaps as
icons instead of paths-to-icons.
2020-03-29 19:37:23 +02:00

40 lines
958 B
C++

#include <AK/SharedBuffer.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ShareableBitmap.h>
#include <LibIPC/Decoder.h>
namespace Gfx {
ShareableBitmap::ShareableBitmap(const Bitmap& bitmap)
: m_bitmap(bitmap.to_bitmap_backed_by_shared_buffer())
{
}
}
namespace IPC {
bool decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
{
i32 shbuf_id = 0;
Gfx::Size size;
if (!decoder.decode(shbuf_id))
return false;
if (!decoder.decode(size))
return false;
if (shbuf_id == -1)
return true;
dbg() << "Decoding a ShareableBitmap with shbuf_id=" << shbuf_id << ", size=" << size;
auto shared_buffer = SharedBuffer::create_from_shbuf_id(shbuf_id);
if (!shared_buffer)
return false;
auto bitmap = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, shared_buffer.release_nonnull(), size);
shareable_bitmap = bitmap->to_shareable_bitmap();
return true;
}
}