1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:57:44 +00:00

LibGfx: Make Gfx::ShareableBitmap use anonymous files instead of shbufs

This commit is contained in:
Andreas Kling 2021-01-15 22:36:36 +01:00
parent adb9ade88d
commit 633915e792
7 changed files with 38 additions and 32 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -44,6 +44,10 @@
#include <stdio.h>
#include <sys/mman.h>
#ifdef __serenity__
# include <serenity.h>
#endif
namespace Gfx {
struct BackingStore {
@ -408,6 +412,22 @@ RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_shared_buffer() const
return bitmap;
}
#ifdef __serenity__
RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_anon_fd() const
{
if (m_anon_fd != -1)
return *this;
auto anon_fd = anon_create(round_up_to_power_of_two(size_in_bytes(), PAGE_SIZE), O_CLOEXEC);
if (anon_fd < 0)
return nullptr;
auto bitmap = Bitmap::create_with_anon_fd(m_format, anon_fd, m_size, ShouldCloseAnonymousFile::No);
if (!bitmap)
return nullptr;
memcpy(bitmap->scanline(0), scanline(0), size_in_bytes());
return bitmap;
}
#endif
Bitmap::~Bitmap()
{
if (m_needs_munmap) {
@ -477,13 +497,11 @@ int Bitmap::shbuf_id() const
return m_shared_buffer ? m_shared_buffer->shbuf_id() : -1;
}
ShareableBitmap Bitmap::to_shareable_bitmap(pid_t peer_pid) const
ShareableBitmap Bitmap::to_shareable_bitmap() const
{
auto bitmap = to_bitmap_backed_by_shared_buffer();
auto bitmap = to_bitmap_backed_by_anon_fd();
if (!bitmap)
return {};
if (peer_pid > 0)
bitmap->shared_buffer()->share_with(peer_pid);
return ShareableBitmap(*bitmap);
}

View file

@ -118,9 +118,10 @@ public:
RefPtr<Gfx::Bitmap> rotated(Gfx::RotationDirection) const;
RefPtr<Gfx::Bitmap> flipped(Gfx::Orientation) const;
RefPtr<Bitmap> to_bitmap_backed_by_shared_buffer() const;
RefPtr<Bitmap> to_bitmap_backed_by_anon_fd() const;
ByteBuffer serialize_to_byte_buffer() const;
ShareableBitmap to_shareable_bitmap(pid_t peer_pid = -1) const;
ShareableBitmap to_shareable_bitmap() const;
~Bitmap();

View file

@ -30,11 +30,12 @@
#include <LibGfx/Size.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
#include <LibIPC/File.h>
namespace Gfx {
ShareableBitmap::ShareableBitmap(const Bitmap& bitmap)
: m_bitmap(bitmap.to_bitmap_backed_by_shared_buffer())
: m_bitmap(bitmap.to_bitmap_backed_by_anon_fd())
{
}
@ -44,7 +45,7 @@ namespace IPC {
bool encode(Encoder& encoder, const Gfx::ShareableBitmap& shareable_bitmap)
{
encoder << shareable_bitmap.shbuf_id();
encoder << IPC::File(shareable_bitmap.anon_fd());
encoder << shareable_bitmap.width();
encoder << shareable_bitmap.height();
return true;
@ -52,23 +53,14 @@ bool encode(Encoder& encoder, const Gfx::ShareableBitmap& shareable_bitmap)
bool decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
{
i32 shbuf_id = 0;
IPC::File anon_file;
Gfx::IntSize size;
if (!decoder.decode(shbuf_id))
if (!decoder.decode(anon_file))
return false;
if (!decoder.decode(size))
return false;
if (shbuf_id == -1)
return true;
dbgln("Decoding a ShareableBitmap with shbuf_id={}, size={}", shbuf_id, 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);
auto bitmap = Gfx::Bitmap::create_with_anon_fd(Gfx::BitmapFormat::RGBA32, anon_file.fd(), size, Gfx::Bitmap::ShouldCloseAnonymousFile::No);
shareable_bitmap = bitmap->to_shareable_bitmap();
return true;
}

View file

@ -39,7 +39,7 @@ public:
bool is_valid() const { return m_bitmap; }
i32 shbuf_id() const { return m_bitmap ? m_bitmap->shbuf_id() : -1; }
int anon_fd() const { return m_bitmap ? m_bitmap->anon_fd() : -1; }
const Bitmap* bitmap() const { return m_bitmap; }
Bitmap* bitmap() { return m_bitmap; }