Compare commits

...

3 Commits

Author SHA1 Message Date
Bananymous 547aeb0170 image: Remove inheritance from Netbpm
This inheritance made no sense
2023-11-29 20:56:05 +02:00
Bananymous ebe465e81e Kernel: cast between inheritance with static_cast
using reinterpret_cast is not a good idea. preferably we would use
dynamic_cast, but that is not possible since kernel is compiled with
-fno-rtti.
2023-11-29 20:50:57 +02:00
Bananymous c10dffd719 Kernel: Make internal framebuffer bpp constexpr defined in libc 2023-11-29 20:07:33 +02:00
11 changed files with 55 additions and 68 deletions

View File

@ -9,9 +9,6 @@
namespace Kernel
{
// Internally we hold 32 bpp buffer (top 8 bits not used)
static constexpr uint32_t bytes_per_pixel_internal = 4;
static uint32_t get_framebuffer_device_index()
{
static uint32_t index = 0;
@ -75,7 +72,7 @@ namespace Kernel
m_video_buffer = TRY(VirtualRange::create_to_vaddr_range(
PageTable::kernel(),
KERNEL_OFFSET, UINTPTR_MAX,
BAN::Math::div_round_up<size_t>(m_width * m_height * bytes_per_pixel_internal, PAGE_SIZE) * PAGE_SIZE,
BAN::Math::div_round_up<size_t>(m_width * m_height * (BANAN_FB_BPP / 8), PAGE_SIZE) * PAGE_SIZE,
PageTable::Flags::ReadWrite | PageTable::Flags::Present,
true
));
@ -94,15 +91,14 @@ namespace Kernel
auto& fb_info = buffer.as<framebuffer_info_t>();
fb_info.width = m_width;
fb_info.height = m_height;
fb_info.bpp = m_bpp;
return sizeof(framebuffer_info_t);
}
if ((size_t)offset >= m_width * m_height * bytes_per_pixel_internal)
if ((size_t)offset >= m_width * m_height * (BANAN_FB_BPP / 8))
return 0;
size_t bytes_to_copy = BAN::Math::min<size_t>(m_width * m_height * bytes_per_pixel_internal - offset, buffer.size());
size_t bytes_to_copy = BAN::Math::min<size_t>(m_width * m_height * (BANAN_FB_BPP / 8) - offset, buffer.size());
memcpy(buffer.data(), reinterpret_cast<void*>(m_video_buffer->vaddr() + offset), bytes_to_copy);
return bytes_to_copy;
@ -112,14 +108,14 @@ namespace Kernel
{
if (offset < 0)
return BAN::Error::from_errno(EINVAL);
if ((size_t)offset >= m_width * m_height * bytes_per_pixel_internal)
if ((size_t)offset >= m_width * m_height * (BANAN_FB_BPP / 8))
return 0;
size_t bytes_to_copy = BAN::Math::min<size_t>(m_width * m_height * bytes_per_pixel_internal - offset, buffer.size());
size_t bytes_to_copy = BAN::Math::min<size_t>(m_width * m_height * (BANAN_FB_BPP / 8) - offset, buffer.size());
memcpy(reinterpret_cast<void*>(m_video_buffer->vaddr() + offset), buffer.data(), bytes_to_copy);
uint32_t first_pixel = offset / bytes_per_pixel_internal;
uint32_t pixel_count = BAN::Math::div_round_up<uint32_t>(bytes_to_copy + (offset % bytes_per_pixel_internal), bytes_per_pixel_internal);
uint32_t first_pixel = offset / (BANAN_FB_BPP / 8);
uint32_t pixel_count = BAN::Math::div_round_up<uint32_t>(bytes_to_copy + (offset % (BANAN_FB_BPP / 8)), (BANAN_FB_BPP / 8));
sync_pixels_linear(first_pixel, pixel_count);
return bytes_to_copy;
@ -129,8 +125,12 @@ namespace Kernel
{
if (x >= m_width || y >= m_height)
return;
auto* video_buffer_u32 = reinterpret_cast<uint32_t*>(m_video_buffer->vaddr());
video_buffer_u32[y * m_width + x] = rgb;
auto* video_buffer_u8 = reinterpret_cast<uint8_t*>(m_video_buffer->vaddr());
video_buffer_u8[(y * m_width + x) * (BANAN_FB_BPP / 8) + 0] = rgb >> 0;
video_buffer_u8[(y * m_width + x) * (BANAN_FB_BPP / 8) + 1] = rgb >> 8;
video_buffer_u8[(y * m_width + x) * (BANAN_FB_BPP / 8) + 2] = rgb >> 16;
if constexpr(BANAN_FB_BPP == 32)
video_buffer_u8[(y * m_width + x) * (BANAN_FB_BPP / 8) + 3] = rgb >> 24;
}
void FramebufferDevice::sync_pixels_full()
@ -143,9 +143,9 @@ namespace Kernel
uint32_t row = i / m_width;
uint32_t idx = i % m_width;
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 0] = video_buffer_u8[i * bytes_per_pixel_internal + 0];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 1] = video_buffer_u8[i * bytes_per_pixel_internal + 1];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 2] = video_buffer_u8[i * bytes_per_pixel_internal + 2];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 0] = video_buffer_u8[i * (BANAN_FB_BPP / 8) + 0];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 1] = video_buffer_u8[i * (BANAN_FB_BPP / 8) + 1];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 2] = video_buffer_u8[i * (BANAN_FB_BPP / 8) + 2];
}
}
@ -164,9 +164,9 @@ namespace Kernel
uint32_t row = (first_pixel + i) / m_width;
uint32_t idx = (first_pixel + i) % m_width;
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 0] = video_buffer_u8[(first_pixel + i) * bytes_per_pixel_internal + 0];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 1] = video_buffer_u8[(first_pixel + i) * bytes_per_pixel_internal + 1];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 2] = video_buffer_u8[(first_pixel + i) * bytes_per_pixel_internal + 2];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 0] = video_buffer_u8[(first_pixel + i) * (BANAN_FB_BPP / 8) + 0];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 1] = video_buffer_u8[(first_pixel + i) * (BANAN_FB_BPP / 8) + 1];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 2] = video_buffer_u8[(first_pixel + i) * (BANAN_FB_BPP / 8) + 2];
}
}
@ -186,9 +186,9 @@ namespace Kernel
{
for (uint32_t idx = top_right_x; idx < top_right_x + width; idx++)
{
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 0] = video_buffer_u8[(row * m_width + idx) * bytes_per_pixel_internal + 0];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 1] = video_buffer_u8[(row * m_width + idx) * bytes_per_pixel_internal + 1];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 2] = video_buffer_u8[(row * m_width + idx) * bytes_per_pixel_internal + 2];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 0] = video_buffer_u8[(row * m_width + idx) * (BANAN_FB_BPP / 8) + 0];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 1] = video_buffer_u8[(row * m_width + idx) * (BANAN_FB_BPP / 8) + 1];
video_memory_u8[(row * m_pitch) + (idx * m_bpp / 8) + 2] = video_buffer_u8[(row * m_width + idx) * (BANAN_FB_BPP / 8) + 2];
}
}
}
@ -224,8 +224,8 @@ namespace Kernel
size = (vaddr - m_vaddr) + m_size;
m_framebuffer->sync_pixels_linear(
(vaddr - m_vaddr) / bytes_per_pixel_internal,
BAN::Math::div_round_up<uint32_t>((vaddr % bytes_per_pixel_internal) + size, bytes_per_pixel_internal)
(vaddr - m_vaddr) / (BANAN_FB_BPP / 8),
BAN::Math::div_round_up<uint32_t>((vaddr % (BANAN_FB_BPP / 8)) + size, (BANAN_FB_BPP / 8))
);
return {};

View File

@ -43,7 +43,7 @@ namespace Kernel
[](BAN::RefPtr<TmpInode> inode)
{
if (inode->is_device())
reinterpret_cast<Device*>(inode.ptr())->update();
static_cast<Device*>(inode.ptr())->update();
return BAN::Iteration::Continue;
}
);
@ -77,7 +77,7 @@ namespace Kernel
{
if (inode->is_device())
if (((Device*)inode.ptr())->is_storage_device())
if (auto ret = reinterpret_cast<StorageDevice*>(inode.ptr())->sync_disk_cache(); ret.is_error())
if (auto ret = static_cast<StorageDevice*>(inode.ptr())->sync_disk_cache(); ret.is_error())
dwarnln("disk sync: {}", ret.error());
return BAN::Iteration::Continue;
}
@ -119,13 +119,13 @@ namespace Kernel
void DevFileSystem::add_device(BAN::RefPtr<Device> device)
{
ASSERT(!device->name().contains('/'));
MUST(reinterpret_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*device, device->name()));
MUST(static_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*device, device->name()));
}
void DevFileSystem::add_inode(BAN::StringView path, BAN::RefPtr<TmpInode> inode)
{
ASSERT(!path.contains('/'));
MUST(reinterpret_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*inode, path));
MUST(static_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*inode, path));
}
void DevFileSystem::for_each_device(const BAN::Function<BAN::Iteration(Device*)>& callback)
@ -136,7 +136,7 @@ namespace Kernel
{
if (!inode->is_device())
return BAN::Iteration::Continue;
return callback(reinterpret_cast<Device*>(inode.ptr()));
return callback(static_cast<Device*>(inode.ptr()));
}
);
}

View File

@ -31,7 +31,7 @@ namespace Kernel
{
auto path = BAN::String::formatted("{}", process.pid());
auto inode = TRY(ProcPidInode::create_new(process, *this, 0555, process.credentials().ruid(), process.credentials().rgid()));
TRY(reinterpret_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*inode, path));
TRY(static_cast<TmpDirectoryInode*>(root_inode().ptr())->link_inode(*inode, path));
return {};
}
@ -40,7 +40,7 @@ namespace Kernel
auto path = BAN::String::formatted("{}", process.pid());
auto inode = MUST(root_inode()->find_inode(path));
reinterpret_cast<ProcPidInode*>(inode.ptr())->cleanup();
static_cast<ProcPidInode*>(inode.ptr())->cleanup();
if (auto ret = root_inode()->unlink(path); ret.is_error())
dwarnln("{}", ret.error());

View File

@ -23,9 +23,9 @@ namespace Kernel
root = root.substring(5);
auto partition_inode = MUST(DevFileSystem::get().root_inode()->find_inode(root));
if (!partition_inode->is_device() || !reinterpret_cast<Device*>(partition_inode.ptr())->is_partition())
if (!partition_inode->is_device() || !static_cast<Device*>(partition_inode.ptr())->is_partition())
Kernel::panic("Specified root '/dev/{}' does not name a partition", root);
s_instance->m_root_fs = MUST(Ext2FS::create(reinterpret_cast<BlockDevice*>(partition_inode.ptr())));
s_instance->m_root_fs = MUST(Ext2FS::create(static_cast<BlockDevice*>(partition_inode.ptr())));
Credentials root_creds { 0, 0, 0, 0 };
MUST(s_instance->mount(root_creds, &DevFileSystem::get(), "/dev"sv));
@ -48,11 +48,11 @@ namespace Kernel
if (!block_device_file.inode->is_device())
return BAN::Error::from_errno(ENOTBLK);
auto* device = reinterpret_cast<Device*>(block_device_file.inode.ptr());
auto* device = static_cast<Device*>(block_device_file.inode.ptr());
if (!device->mode().ifblk())
return BAN::Error::from_errno(ENOTBLK);
auto* block_device = reinterpret_cast<BlockDevice*>(device);
auto* block_device = static_cast<BlockDevice*>(device);
auto* file_system = TRY(Ext2FS::create(block_device));
return mount(credentials, file_system, target);
}

View File

@ -41,7 +41,7 @@ namespace Kernel
auto inode = inode_or_error.release_value();
if (inode->mode().iflnk())
MUST(reinterpret_cast<TmpSymlinkInode*>(inode.ptr())->set_link_target(name()));
MUST(static_cast<TmpSymlinkInode&>(*inode.ptr()).set_link_target(name()));
}
BAN::ErrorOr<void> TTY::tty_ctrl(int command, int flags)

View File

@ -7,11 +7,12 @@ __BEGIN_DECLS
#include <stdint.h>
#define BANAN_FB_BPP 32
struct framebuffer_info_t
{
uint32_t width;
uint32_t height;
uint8_t bpp;
};
__END_DECLS

View File

@ -70,7 +70,7 @@ BAN::UniqPtr<Image> Image::load_from_file(BAN::StringView path)
case 0x3350:
case 0x3250:
case 0x3150:
if (auto res = Netbpm::create(addr, st.st_size); res.is_error())
if (auto res = load_netbpm(addr, st.st_size); res.is_error())
fprintf(stderr, "%s\n", strerror(res.error().get_error_code()));
else
image = res.release_value();
@ -103,14 +103,9 @@ bool Image::render_to_framebuffer()
return false;
}
if (fb_info.bpp != 24 && fb_info.bpp != 32)
{
fprintf(stderr, "unsupported framebuffer bpp\n");
close(fd);
return false;
}
ASSERT(BANAN_FB_BPP == 24 || BANAN_FB_BPP == 32);
size_t mmap_size = fb_info.height * fb_info.width * fb_info.bpp / 8;
size_t mmap_size = fb_info.height * fb_info.width * BANAN_FB_BPP / 8;
void* mmap_addr = mmap(nullptr, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (mmap_addr == MAP_FAILED)
@ -126,11 +121,11 @@ bool Image::render_to_framebuffer()
{
for (uint64_t x = 0; x < BAN::Math::min<uint64_t>(width(), fb_info.width); x++)
{
u8_fb[(y * fb_info.width + x) * fb_info.bpp / 8 + 0] = m_bitmap[y * width() + x].r;
u8_fb[(y * fb_info.width + x) * fb_info.bpp / 8 + 1] = m_bitmap[y * width() + x].g;
u8_fb[(y * fb_info.width + x) * fb_info.bpp / 8 + 2] = m_bitmap[y * width() + x].b;
if (fb_info.bpp == 32)
u8_fb[(y * fb_info.width + x) * fb_info.bpp / 8 + 3] = m_bitmap[y * width() + x].a;
u8_fb[(y * fb_info.width + x) * BANAN_FB_BPP / 8 + 0] = m_bitmap[y * width() + x].r;
u8_fb[(y * fb_info.width + x) * BANAN_FB_BPP / 8 + 1] = m_bitmap[y * width() + x].g;
u8_fb[(y * fb_info.width + x) * BANAN_FB_BPP / 8 + 2] = m_bitmap[y * width() + x].b;
if constexpr(BANAN_FB_BPP == 32)
u8_fb[(y * fb_info.width + x) * BANAN_FB_BPP / 8 + 3] = m_bitmap[y * width() + x].a;
}
}

View File

@ -22,15 +22,17 @@ public:
bool render_to_framebuffer();
protected:
private:
Image(uint64_t width, uint64_t height, BAN::Vector<Color>&& bitmap)
: m_width(width)
, m_height(height)
, m_bitmap(BAN::move(bitmap))
{ }
protected:
private:
const uint64_t m_width;
const uint64_t m_height;
const BAN::Vector<Color> m_bitmap;
friend class BAN::UniqPtr<Image>;
};

View File

@ -29,7 +29,7 @@ BAN::Optional<uint64_t> parse_u64(const uint8_t*& data, size_t data_size)
return {};
}
BAN::ErrorOr<BAN::UniqPtr<Netbpm>> Netbpm::create(const void* mmap_addr, size_t size)
BAN::ErrorOr<BAN::UniqPtr<Image>> load_netbpm(const void* mmap_addr, size_t size)
{
if (size < 11)
{
@ -88,7 +88,7 @@ BAN::ErrorOr<BAN::UniqPtr<Netbpm>> Netbpm::create(const void* mmap_addr, size_t
printf("Netbpm image %llux%llu\n", *width, *height);
BAN::Vector<Color> bitmap;
BAN::Vector<Image::Color> bitmap;
TRY(bitmap.resize(*width * *height));
// Fill bitmap
@ -104,5 +104,5 @@ BAN::ErrorOr<BAN::UniqPtr<Netbpm>> Netbpm::create(const void* mmap_addr, size_t
}
}
return TRY(BAN::UniqPtr<Netbpm>::create(*width, *height, BAN::move(bitmap)));
return TRY(BAN::UniqPtr<Image>::create(*width, *height, BAN::move(bitmap)));
}

View File

@ -1,14 +1,3 @@
#include "Image.h"
class Netbpm : public Image
{
public:
static BAN::ErrorOr<BAN::UniqPtr<Netbpm>> create(const void* mmap_addr, size_t size);
private:
Netbpm(uint64_t width, uint64_t height, BAN::Vector<Color>&& bitmap)
: Image(width, height, BAN::move(bitmap))
{ }
friend class BAN::UniqPtr<Netbpm>;
};
BAN::ErrorOr<BAN::UniqPtr<Image>> load_netbpm(const void* mmap_addr, size_t size);

View File

@ -19,7 +19,7 @@ int main()
return 1;
}
size_t fb_size = fb_info.width * fb_info.height * fb_info.bpp / 8;
size_t fb_size = fb_info.width * fb_info.height * BANAN_FB_BPP / 8;
void* addr = mmap(nullptr, fb_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED)