feature (kernel): Implemented a very basic double buffered graphics renderer

This commit is contained in:
antifallobst 2023-02-19 22:25:45 +01:00
parent 81e4f2abb2
commit 52e4ef8a49
5 changed files with 160 additions and 24 deletions

View File

@ -0,0 +1,28 @@
/* Copyright (C) Antifallobst <antifallobst@systemausfall.org>
*
* NoxOS is free software:
* you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* NoxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef NOX_COLOR_H
#define NOX_COLOR_H
#include "utils/stdtypes.h"
typedef struct {
uint8_t alpha;
uint8_t red;
uint8_t green;
uint8_t blue;
} color_argb_T;
#endif //NOX_COLOR_H

View File

@ -0,0 +1,30 @@
/* Copyright (C) Antifallobst <antifallobst@systemausfall.org>
*
* NoxOS is free software:
* you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* NoxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef NOX_FRAMEBUFFER_H
#define NOX_FRAMEBUFFER_H
#include "utils/stdtypes.h"
typedef struct {
void* address;
uint64_t width;
uint64_t height;
uint64_t pitch;
uint16_t bits_per_pixel;
uint8_t bytes_per_pixel;
} framebuffer_T;
#endif //NOX_FRAMEBUFFER_H

View File

@ -0,0 +1,35 @@
/* Copyright (C) Antifallobst <antifallobst@systemausfall.org>
*
* NoxOS is free software:
* you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* NoxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef NOX_RENDERER_H
#define NOX_RENDERER_H
#include "utils/stdtypes.h"
#include "drivers/graphics/color.h"
#include "drivers/graphics/framebuffer.h"
#include "boot/boot_info.h"
typedef struct {
framebuffer_T framebuffer;
uint8_t* back_buffer;
uint64_t buffer_size;
} graphics_renderer_T;
void graphics_renderer_set_pixel (uint32_t x, uint32_t y, color_argb_T color);
color_argb_T graphics_renderer_get_pixel (uint32_t x, uint32_t y);
void graphics_renderer_update ();
void graphics_renderer_init (boot_info_T* boot_info);
#endif //NOX_RENDERER_H

View File

@ -0,0 +1,59 @@
/* Copyright (C) Antifallobst <antifallobst@systemausfall.org>
*
* NoxOS is free software:
* you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* NoxOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/
#include "drivers/graphics/renderer.h"
#include "utils/memory.h"
#include "utils/math.h"
graphics_renderer_T g_renderer;
void graphics_renderer_set_pixel(uint32_t x, uint32_t y, color_argb_T color) {
uint64_t position = (y * g_renderer.framebuffer.width * g_renderer.framebuffer.bytes_per_pixel) + (x * g_renderer.framebuffer.bytes_per_pixel);
g_renderer.back_buffer[position] = color.red;
g_renderer.back_buffer[position+1] = color.green;
g_renderer.back_buffer[position+2] = color.blue;
}
color_argb_T graphics_renderer_get_pixel(uint32_t x, uint32_t y) {
uint64_t position = (y * g_renderer.framebuffer.width * g_renderer.framebuffer.bytes_per_pixel) + (x * g_renderer.framebuffer.bytes_per_pixel);
color_argb_T color;
color.alpha = 0xFF;
color.red = g_renderer.back_buffer[position];
color.green = g_renderer.back_buffer[position+1];
color.blue = g_renderer.back_buffer[position+2];
return color;
}
void graphics_renderer_update() {
memory_copy(g_renderer.back_buffer, g_renderer.framebuffer.address, g_renderer.buffer_size);
}
void graphics_renderer_init(boot_info_T* boot_info_T) {
struct limine_framebuffer* framebuffer = boot_info_T->framebuffer->framebuffers[0];
g_renderer.framebuffer.address = framebuffer->address;
g_renderer.framebuffer.width = framebuffer->width;
g_renderer.framebuffer.height = framebuffer->height;
g_renderer.framebuffer.pitch = framebuffer->pitch;
g_renderer.framebuffer.bits_per_pixel = framebuffer->bpp;
g_renderer.framebuffer.bytes_per_pixel = CEIL_TO(framebuffer->bpp, 8) / 8;
g_renderer.buffer_size = g_renderer.framebuffer.width * g_renderer.framebuffer.height * g_renderer.framebuffer.bytes_per_pixel;
g_renderer.back_buffer = memory_allocate(g_renderer.buffer_size);
memory_set(g_renderer.back_buffer, 0, g_renderer.buffer_size);
}

View File

@ -21,6 +21,7 @@
#include "mm/page_frame.h" #include "mm/page_frame.h"
#include "mm/page_map.h" #include "mm/page_map.h"
#include "drivers/time/pit.h" #include "drivers/time/pit.h"
#include "drivers/graphics/renderer.h"
#include "proc/scheduler.h" #include "proc/scheduler.h"
#include "utils/io.h" #include "utils/io.h"
@ -41,27 +42,10 @@ void kernel_init(boot_info_T* boot_info) {
paging_init(); paging_init();
memory_allocator_init((void*)0x100000000000); memory_allocator_init((void*)0x100000000000);
graphics_renderer_init(boot_info);
scheduler_init(); scheduler_init();
} }
void test_a() {
while (true) {
io_out_byte(LOG_PORT, 'A');
}
}
void test_b() {
while (true) {
io_out_byte(LOG_PORT, 'B');
}
}
void test_c() {
while (true) {
io_out_byte(LOG_PORT, 'C');
}
}
void kmain(boot_info_T boot_info) { void kmain(boot_info_T boot_info) {
limine_terminal_print(&boot_info, "Booting NoxOS...\n"); limine_terminal_print(&boot_info, "Booting NoxOS...\n");
@ -72,13 +56,13 @@ void kmain(boot_info_T boot_info) {
limine_terminal_print(&boot_info, "Kernel initialized\n"); limine_terminal_print(&boot_info, "Kernel initialized\n");
log(LOG_INFO, "!=====[ Kernel Initialized ]=====!\n"); log(LOG_INFO, "!=====[ Kernel Initialized ]=====!\n");
thread_T* thread = thread_spawn(test_b); for (int i = 0; i < 500; i++) {
thread_start(thread); graphics_renderer_set_pixel(i, i, (color_argb_T){0xFF, 0x00, 0xFF, 0x00});
}
graphics_renderer_update();
thread_T* thread1 = thread_spawn(test_c); color_argb_T color = graphics_renderer_get_pixel(0, 0);
thread_start(thread1); log(LOG_DEBUG, "ALPHA[0x%xb] RED[0x%xb] GREEN[0x%xb] BLUE[0x%xb]", color.alpha, color.red, color.green, color.blue);
test_a();
CORE_HALT_FOREVER CORE_HALT_FOREVER
} }