2019-01-08 20:06:04 -05:00
|
|
|
#include "GraphicsBitmap.h"
|
2019-01-14 14:00:42 -05:00
|
|
|
|
|
|
|
#ifdef KERNEL
|
2019-01-16 10:03:50 -05:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/MemoryManager.h>
|
2019-01-25 23:28:02 -05:00
|
|
|
#include <WindowServer/WSMessageLoop.h>
|
2019-01-14 14:00:42 -05:00
|
|
|
#endif
|
2019-01-12 18:27:25 -05:00
|
|
|
|
2019-02-07 17:13:47 -05:00
|
|
|
#ifdef USERLAND
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2019-01-14 14:00:42 -05:00
|
|
|
#ifdef KERNEL
|
2019-02-11 03:47:10 -05:00
|
|
|
RetainPtr<GraphicsBitmap> GraphicsBitmap::create_kernel_only(const Size& size)
|
|
|
|
{
|
|
|
|
return adopt(*new GraphicsBitmap(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsBitmap::GraphicsBitmap(const Size& size)
|
|
|
|
: m_size(size)
|
|
|
|
, m_pitch(size.width() * sizeof(RGBA32))
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
size_t size_in_bytes = size.width() * size.height() * sizeof(RGBA32);
|
|
|
|
auto vmo = VMObject::create_anonymous(size_in_bytes);
|
|
|
|
auto& server = WSMessageLoop::the().server_process();
|
|
|
|
m_server_region = server.allocate_region_with_vmo(LinearAddress(), size_in_bytes, move(vmo), 0, "GraphicsBitmap (server)", true, false);
|
|
|
|
m_server_region->set_shared(true);
|
|
|
|
m_server_region->set_is_bitmap(true);
|
|
|
|
m_data = (RGBA32*)m_server_region->laddr().as_ptr();
|
2019-01-08 21:51:34 -05:00
|
|
|
}
|
2019-01-14 14:00:42 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
RetainPtr<GraphicsBitmap> GraphicsBitmap::create_wrapper(const Size& size, RGBA32* data)
|
|
|
|
{
|
|
|
|
return adopt(*new GraphicsBitmap(size, data));
|
|
|
|
}
|
2019-01-08 21:51:34 -05:00
|
|
|
|
2019-02-07 17:13:47 -05:00
|
|
|
RetainPtr<GraphicsBitmap> GraphicsBitmap::load_from_file(const String& path, const Size& size)
|
|
|
|
{
|
2019-02-08 10:49:05 -05:00
|
|
|
RGBA32* mapped_data = nullptr;
|
2019-02-07 17:13:47 -05:00
|
|
|
#ifdef USERLAND
|
|
|
|
int fd = open(path.characters(), O_RDONLY, 0644);
|
|
|
|
if (fd < 0) {
|
|
|
|
dbgprintf("open(%s) got fd=%d, failed: %s\n", path.characters(), fd, strerror(errno));
|
|
|
|
perror("open");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-08 10:49:05 -05:00
|
|
|
mapped_data = (RGBA32*)mmap(nullptr, size.area() * 4, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
if (mapped_data == MAP_FAILED) {
|
2019-02-07 17:13:47 -05:00
|
|
|
int rc = close(fd);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int error;
|
|
|
|
auto descriptor = VFS::the().open(path, error, 0, 0, *VFS::the().root_inode());
|
|
|
|
if (!descriptor) {
|
|
|
|
kprintf("Failed to load GraphicsBitmap from file (%s)\n", path.characters());
|
2019-02-08 10:49:05 -05:00
|
|
|
return nullptr;
|
2019-02-07 17:13:47 -05:00
|
|
|
}
|
2019-02-08 03:48:28 -05:00
|
|
|
auto* region = WSMessageLoop::the().server_process().allocate_file_backed_region(LinearAddress(), size.area() * 4, descriptor->inode(), ".rgb file", /*readable*/true, /*writable*/false);
|
2019-02-08 10:49:05 -05:00
|
|
|
mapped_data = (RGBA32*)region->laddr().get();
|
2019-02-07 17:13:47 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef USERLAND
|
|
|
|
int rc = close(fd);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
#endif
|
2019-02-08 10:49:05 -05:00
|
|
|
auto bitmap = create_wrapper(size, mapped_data);
|
2019-02-07 17:13:47 -05:00
|
|
|
#ifdef KERNEL
|
|
|
|
bitmap->m_server_region = region;
|
|
|
|
#else
|
|
|
|
bitmap->m_mmaped = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2019-01-09 23:36:32 -05:00
|
|
|
GraphicsBitmap::GraphicsBitmap(const Size& size, RGBA32* data)
|
2019-01-08 21:51:34 -05:00
|
|
|
: m_size(size)
|
2019-01-09 23:36:32 -05:00
|
|
|
, m_data(data)
|
2019-01-12 15:29:05 -05:00
|
|
|
, m_pitch(size.width() * sizeof(RGBA32))
|
2019-01-08 21:51:34 -05:00
|
|
|
{
|
2019-01-08 20:06:04 -05:00
|
|
|
}
|
|
|
|
|
2019-02-16 06:13:43 -05:00
|
|
|
RetainPtr<GraphicsBitmap> GraphicsBitmap::create_with_shared_buffer(int shared_buffer_id, const Size& size, RGBA32* data)
|
|
|
|
{
|
|
|
|
if (!data) {
|
|
|
|
#ifdef KERNEL
|
|
|
|
void* shared_buffer = current->sys$get_shared_buffer(shared_buffer_id);
|
|
|
|
#else
|
|
|
|
void* shared_buffer = get_shared_buffer(shared_buffer_id);
|
|
|
|
#endif
|
|
|
|
if (!shared_buffer || shared_buffer == (void*)-1)
|
|
|
|
return nullptr;
|
|
|
|
data = (RGBA32*)shared_buffer;
|
|
|
|
}
|
|
|
|
return adopt(*new GraphicsBitmap(shared_buffer_id, size, data));
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsBitmap::GraphicsBitmap(int shared_buffer_id, const Size& size, RGBA32* data)
|
|
|
|
: m_size(size)
|
|
|
|
, m_data(data)
|
|
|
|
, m_pitch(size.width() * sizeof(RGBA32))
|
|
|
|
, m_shared_buffer_id(shared_buffer_id)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-08 20:06:04 -05:00
|
|
|
GraphicsBitmap::~GraphicsBitmap()
|
|
|
|
{
|
2019-01-14 14:00:42 -05:00
|
|
|
#ifdef KERNEL
|
2019-01-14 09:25:34 -05:00
|
|
|
if (m_server_region)
|
2019-01-25 23:28:02 -05:00
|
|
|
WSMessageLoop::the().server_process().deallocate_region(*m_server_region);
|
2019-02-07 17:13:47 -05:00
|
|
|
#else
|
|
|
|
if (m_mmaped) {
|
|
|
|
int rc = munmap(m_data, m_size.area() * 4);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|
2019-01-14 14:00:42 -05:00
|
|
|
#endif
|
2019-02-16 06:13:43 -05:00
|
|
|
if (m_shared_buffer_id != -1) {
|
|
|
|
int rc;
|
|
|
|
#ifdef KERNEL
|
|
|
|
rc = current->sys$release_shared_buffer(m_shared_buffer_id);
|
|
|
|
#else
|
|
|
|
rc = release_shared_buffer(m_shared_buffer_id);
|
|
|
|
#endif
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|
2019-01-08 21:51:34 -05:00
|
|
|
m_data = nullptr;
|
2019-01-08 20:06:04 -05:00
|
|
|
}
|
|
|
|
|