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

Add a C++ helper class for working with shared buffers.

This is a bit more comfortable than passing the shared buffer ID manually
everywhere and keeping track of size etc.
This commit is contained in:
Andreas Kling 2019-03-08 12:22:55 +01:00
parent 0b5d5fc3c9
commit eda0866992
14 changed files with 186 additions and 46 deletions

View file

@ -58,23 +58,17 @@ GraphicsBitmap::GraphicsBitmap(Format format, const Size& size, RGBA32* data)
{
}
RetainPtr<GraphicsBitmap> GraphicsBitmap::create_with_shared_buffer(Format format, int shared_buffer_id, const Size& size, RGBA32* data)
RetainPtr<GraphicsBitmap> GraphicsBitmap::create_with_shared_buffer(Format format, Retained<SharedBuffer>&& shared_buffer, const Size& size)
{
if (!data) {
void* shared_buffer = get_shared_buffer(shared_buffer_id);
if (!shared_buffer || shared_buffer == (void*)-1)
return nullptr;
data = (RGBA32*)shared_buffer;
}
return adopt(*new GraphicsBitmap(format, shared_buffer_id, size, data));
return adopt(*new GraphicsBitmap(format, move(shared_buffer), size));
}
GraphicsBitmap::GraphicsBitmap(Format format, int shared_buffer_id, const Size& size, RGBA32* data)
GraphicsBitmap::GraphicsBitmap(Format format, Retained<SharedBuffer>&& shared_buffer, const Size& size)
: m_size(size)
, m_data(data)
, m_data((RGBA32*)shared_buffer->data())
, m_pitch(size.width() * sizeof(RGBA32))
, m_format(format)
, m_shared_buffer_id(shared_buffer_id)
, m_shared_buffer(move(shared_buffer))
{
}
@ -84,10 +78,6 @@ GraphicsBitmap::~GraphicsBitmap()
int rc = munmap(m_data, m_size.area() * 4);
ASSERT(rc == 0);
}
if (m_shared_buffer_id != -1) {
int rc = release_shared_buffer(m_shared_buffer_id);
ASSERT(rc == 0);
}
m_data = nullptr;
}

View file

@ -6,6 +6,7 @@
#include <AK/Retainable.h>
#include <AK/RetainPtr.h>
#include <AK/AKString.h>
#include <SharedBuffer.h>
class GraphicsBitmap : public Retainable<GraphicsBitmap> {
public:
@ -14,7 +15,7 @@ public:
static Retained<GraphicsBitmap> create(Format, const Size&);
static Retained<GraphicsBitmap> create_wrapper(Format, const Size&, RGBA32*);
static RetainPtr<GraphicsBitmap> load_from_file(Format, const String& path, const Size&);
static RetainPtr<GraphicsBitmap> create_with_shared_buffer(Format, int shared_buffer_id, const Size&, RGBA32* buffer = nullptr);
static RetainPtr<GraphicsBitmap> create_with_shared_buffer(Format, Retained<SharedBuffer>&&, const Size&);
~GraphicsBitmap();
RGBA32* scanline(int y);
@ -25,21 +26,21 @@ public:
int width() const { return m_size.width(); }
int height() const { return m_size.height(); }
size_t pitch() const { return m_pitch; }
int shared_buffer_id() const { return m_shared_buffer_id; }
int shared_buffer_id() const { return m_shared_buffer ? m_shared_buffer->shared_buffer_id() : -1; }
bool has_alpha_channel() const { return m_format == Format::RGBA32; }
private:
GraphicsBitmap(Format, const Size&);
GraphicsBitmap(Format, const Size&, RGBA32*);
GraphicsBitmap(Format, int shared_buffer_id, const Size&, RGBA32*);
GraphicsBitmap(Format, Retained<SharedBuffer>&&, const Size&);
Size m_size;
RGBA32* m_data { nullptr };
size_t m_pitch { 0 };
Format m_format { Format::Invalid };
bool m_mmaped { false };
int m_shared_buffer_id { -1 };
RetainPtr<SharedBuffer> m_shared_buffer;
};
inline RGBA32* GraphicsBitmap::scanline(int y)