123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- #include "screen_manager.h"
- #include <common/string.h>
- #include <driver/multiboot2/multiboot2.h>
- #include <common/kprint.h>
- #include <common/spinlock.h>
- #include <mm/mm.h>
- #include <mm/slab.h>
- #include <driver/uart/uart.h>
- #include <driver/video/video.h>
- extern struct scm_buffer_info_t video_frame_buffer_info;
- static struct List scm_framework_list;
- static spinlock_t scm_register_lock;
- static spinlock_t scm_screen_own_lock = {1};
- static struct scm_ui_framework_t *__current_framework;
- static uint32_t scm_ui_max_id = 0;
- static bool __scm_alloc_enabled = false;
- static bool __scm_double_buffer_enabled = false;
- static struct scm_buffer_info_t *__create_buffer(uint64_t type)
- {
-
- if (unlikely(__scm_double_buffer_enabled == false))
- return &video_frame_buffer_info;
- struct scm_buffer_info_t *buf = (struct scm_buffer_info_t *)kmalloc(sizeof(struct scm_buffer_info_t), 0);
- if (buf == NULL)
- return (void *)-ENOMEM;
- memset(buf, 0, sizeof(struct scm_buffer_info_t));
- buf->bit_depth = video_frame_buffer_info.bit_depth;
- buf->flags = SCM_BF_DB;
- if (type & SCM_BF_PIXEL)
- buf->flags |= SCM_BF_PIXEL;
- else
- buf->flags |= SCM_BF_TEXT;
- buf->height = video_frame_buffer_info.height;
- buf->width = video_frame_buffer_info.width;
- buf->size = video_frame_buffer_info.size;
- struct Page *p = alloc_pages(ZONE_NORMAL, PAGE_2M_ALIGN(video_frame_buffer_info.size) / PAGE_2M_SIZE, 0);
- if (p == NULL)
- goto failed;
- buf->vaddr = (uint64_t)phys_2_virt(p->addr_phys);
- return buf;
- failed:;
- kfree(buf);
- return (void *)-ENOMEM;
- }
- static int __destroy_buffer(struct scm_buffer_info_t *buf)
- {
-
- if (unlikely(buf == &video_frame_buffer_info || buf == NULL))
- return -EINVAL;
- if (unlikely(buf->vaddr == NULL))
- return -EINVAL;
- if (unlikely(verify_area(buf->vaddr, buf->size) == true))
- return -EINVAL;
-
- if (buf->flags & SCM_BF_FB)
- return -EINVAL;
-
- free_pages(Phy_to_2M_Page(virt_2_phys(buf->vaddr)), PAGE_2M_ALIGN(video_frame_buffer_info.size) / PAGE_2M_SIZE);
- return 0;
- }
- void scm_init()
- {
- list_init(&scm_framework_list);
- spin_init(&scm_register_lock);
- spin_init(&scm_screen_own_lock);
- io_mfence();
- scm_ui_max_id = 0;
- __scm_alloc_enabled = false;
- __scm_double_buffer_enabled = false;
- __current_framework = NULL;
- }
- static int __check_ui_param(const char *name, const uint8_t type, const struct scm_ui_framework_operations_t *ops)
- {
- if (name == NULL)
- return -EINVAL;
- if ((type == SCM_FRAMWORK_TYPE_GUI || type == SCM_FRAMWORK_TYPE_TEXT) == 0)
- return -EINVAL;
- if (ops == NULL)
- return -EINVAL;
- if (ops->install == NULL || ops->uninstall == NULL || ops->enable == NULL || ops->disable == NULL || ops->change == NULL)
- return -EINVAL;
- return 0;
- }
- int scm_register_alloc(const char *name, const uint8_t type, struct scm_ui_framework_operations_t *ops)
- {
-
- if (unlikely(__scm_alloc_enabled == false))
- return -EAGAIN;
-
- if (__check_ui_param(name, type, ops) != 0)
- return -EINVAL;
- struct scm_ui_framework_t *ui = (struct scm_ui_framework_t *)kmalloc(sizeof(struct scm_ui_framework_t *), 0);
- memset(ui, 0, sizeof(struct scm_ui_framework_t));
- strncpy(ui->name, name, 15);
- ui->type = type;
- ui->ui_ops = ops;
- list_init(&ui->list);
- spin_lock(&scm_register_lock);
- ui->id = scm_ui_max_id++;
- spin_unlock(&scm_register_lock);
-
- ui->buf = __create_buffer(ui->type);
- if ((uint64_t)(ui->buf) == (uint64_t)-ENOMEM)
- {
- kfree(ui);
- return -ENOMEM;
- }
-
- list_add(&scm_framework_list, &ui->list);
-
- ui->ui_ops->install(ui->buf);
- ui->ui_ops->enable(NULL);
- if (__current_framework == NULL)
- return scm_framework_enable(ui);
- return 0;
- }
- int scm_register(struct scm_ui_framework_t *ui)
- {
- if (ui == NULL)
- return -EINVAL;
- if (__check_ui_param(ui->name, ui->type, ui->ui_ops) != 0)
- return -EINVAL;
- list_init(&ui->list);
- spin_lock(&scm_register_lock);
- ui->id = scm_ui_max_id++;
- spin_unlock(&scm_register_lock);
- ui->buf = __create_buffer(ui->type);
- if ((uint64_t)(ui->buf) == (uint64_t)-ENOMEM)
- return -ENOMEM;
-
- list_add(&scm_framework_list, &ui->list);
-
- ui->ui_ops->install(ui->buf);
- ui->ui_ops->enable(NULL);
- if (__current_framework == NULL)
- return scm_framework_enable(ui);
- return 0;
- }
- int scm_unregister(struct scm_ui_framework_t *ui)
- {
- }
- int scm_unregister_alloc(struct scm_ui_framework_t *ui)
- {
- }
- int scm_enable_alloc()
- {
- __scm_alloc_enabled = true;
- return 0;
- }
- int scm_enable_double_buffer()
- {
- if (__scm_double_buffer_enabled == true)
- return 0;
- __scm_double_buffer_enabled = true;
- if (list_empty(&scm_framework_list))
- return 0;
-
- struct scm_ui_framework_t *ptr = container_of(list_next(&scm_framework_list), struct scm_ui_framework_t, list);
- do
- {
- if (ptr->buf == &video_frame_buffer_info)
- {
- uart_send_str(COM1, "##init double buffer##");
- struct scm_buffer_info_t *buf = __create_buffer(SCM_BF_DB | SCM_BF_PIXEL);
- if ((uint64_t)(buf) == (uint64_t)-ENOMEM)
- return -ENOMEM;
- uart_send_str(COM1, "##to change double buffer##");
- if (ptr->ui_ops->change(buf) != 0)
- {
- __destroy_buffer(buf);
- kfree(buf);
- }
- }
- } while (list_next(&ptr->list) != &scm_framework_list);
-
- video_set_refresh_target(__current_framework->buf);
-
- video_reinitialize(true);
- uart_send_str(COM1, "##initialized double buffer##");
- return 0;
- }
- int scm_framework_enable(struct scm_ui_framework_t *ui)
- {
- if (ui->buf->vaddr == NULL)
- return -EINVAL;
- spin_lock(&scm_screen_own_lock);
- int retval = 0;
- if (__scm_double_buffer_enabled == true)
- {
- retval = video_set_refresh_target(ui->buf);
- if (retval == 0)
- __current_framework = ui;
- }
- else
- __current_framework = ui;
- spin_unlock(&scm_screen_own_lock);
- return retval;
- }
- void scm_reinit()
- {
- scm_enable_alloc();
- video_reinitialize(false);
-
-
- struct scm_ui_framework_t *ptr = container_of(list_next(&scm_framework_list), struct scm_ui_framework_t, list);
- do
- {
- if (ptr->buf == &video_frame_buffer_info)
- {
- ptr->ui_ops->change(&video_frame_buffer_info);
- }
- } while (list_next(&ptr->list) != &scm_framework_list);
- return;
- }
|