feat(osal): add SDL2 based threading and OS support (#7457)

Co-authored-by: ming <sungod@cheemer.org>
This commit is contained in:
M1NG
2024-12-18 18:04:18 +08:00
committed by GitHub
parent bb54fc50c2
commit bdb4d944de
6 changed files with 244 additions and 0 deletions

View File

@@ -105,6 +105,7 @@
* - LV_OS_RTTHREAD
* - LV_OS_WINDOWS
* - LV_OS_MQX
* - LV_OS_SDL2
* - LV_OS_CUSTOM */
#define LV_USE_OS LV_OS_NONE

View File

@@ -38,6 +38,7 @@ fout.write(
#define LV_OS_RTTHREAD 4
#define LV_OS_WINDOWS 5
#define LV_OS_MQX 6
#define LV_OS_SDL2 7
#define LV_OS_CUSTOM 255
#define LV_STDLIB_BUILTIN 0

View File

@@ -16,6 +16,7 @@
#define LV_OS_RTTHREAD 4
#define LV_OS_WINDOWS 5
#define LV_OS_MQX 6
#define LV_OS_SDL2 7
#define LV_OS_CUSTOM 255
#define LV_STDLIB_BUILTIN 0
@@ -263,6 +264,7 @@
* - LV_OS_RTTHREAD
* - LV_OS_WINDOWS
* - LV_OS_MQX
* - LV_OS_SDL2
* - LV_OS_CUSTOM */
#ifndef LV_USE_OS
#ifdef CONFIG_LV_USE_OS

View File

@@ -35,6 +35,8 @@ extern "C" {
#include "lv_windows.h"
#elif LV_USE_OS == LV_OS_MQX
#include "lv_mqx.h"
#elif LV_USE_OS == LV_OS_SDL2
#include "lv_sdl2.h"
#elif LV_USE_OS == LV_OS_CUSTOM
#include LV_OS_CUSTOM_INCLUDE
#endif

183
src/osal/lv_sdl2.c Normal file
View File

@@ -0,0 +1,183 @@
/**
* @file lv_sdl2.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_os.h"
#if LV_USE_OS == LV_OS_SDL2
#include <errno.h>
#include "../misc/lv_log.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static int generic_callback(void * user_data);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
lv_result_t lv_thread_init(lv_thread_t * thread, lv_thread_prio_t prio, void (*callback)(void *), size_t stack_size,
void * user_data)
{
LV_UNUSED(prio);
thread->callback = callback;
thread->user_data = user_data;
thread->thread = SDL_CreateThreadWithStackSize(generic_callback, "LVGL", stack_size, thread);
if(thread->thread == NULL) {
LV_LOG_ERROR("Error: %s", SDL_GetError());
return LV_RESULT_INVALID;
}
return LV_RESULT_OK;
}
lv_result_t lv_thread_delete(lv_thread_t * thread)
{
int ret;
SDL_WaitThread(thread->thread, &ret);
if(ret != 0) {
LV_LOG_ERROR("Error: %d", ret);
return LV_RESULT_INVALID;
}
return LV_RESULT_OK;
}
lv_result_t lv_mutex_init(lv_mutex_t * mutex)
{
*mutex = SDL_CreateMutex();
if(*mutex == NULL) {
LV_LOG_ERROR("Error: %s", SDL_GetError());
return LV_RESULT_INVALID;
}
else {
return LV_RESULT_OK;
}
}
lv_result_t lv_mutex_lock(lv_mutex_t * mutex)
{
int ret = SDL_LockMutex(*mutex);
if(ret) {
LV_LOG_ERROR("Error: %d", ret);
return LV_RESULT_INVALID;
}
else {
return LV_RESULT_OK;
}
}
lv_result_t lv_mutex_lock_isr(lv_mutex_t * mutex)
{
int ret = SDL_LockMutex(*mutex);
if(ret) {
LV_LOG_ERROR("Error: %d", ret);
return LV_RESULT_INVALID;
}
else {
return LV_RESULT_OK;
}
}
lv_result_t lv_mutex_unlock(lv_mutex_t * mutex)
{
int ret = SDL_UnlockMutex(*mutex);
if(ret) {
LV_LOG_ERROR("Error: %d", ret);
return LV_RESULT_INVALID;
}
else {
return LV_RESULT_OK;
}
}
lv_result_t lv_mutex_delete(lv_mutex_t * mutex)
{
SDL_DestroyMutex(*mutex);
return LV_RESULT_OK;
}
lv_result_t lv_thread_sync_init(lv_thread_sync_t * sync)
{
sync->mutex = SDL_CreateMutex();
if(sync->mutex == NULL) {
LV_LOG_ERROR("Error: %s", SDL_GetError());
return LV_RESULT_INVALID;
}
sync->cond = SDL_CreateCond();
if(sync->cond == NULL) {
LV_LOG_ERROR("Error: %s", SDL_GetError());
return LV_RESULT_INVALID;
}
sync->v = false;
return LV_RESULT_OK;
}
lv_result_t lv_thread_sync_wait(lv_thread_sync_t * sync)
{
SDL_LockMutex(sync->mutex);
while(!sync->v) {
SDL_CondWait(sync->cond, sync->mutex);
}
sync->v = false;
SDL_UnlockMutex(sync->mutex);
return LV_RESULT_OK;
}
lv_result_t lv_thread_sync_signal(lv_thread_sync_t * sync)
{
SDL_LockMutex(sync->mutex);
sync->v = true;
SDL_CondSignal(sync->cond);
SDL_UnlockMutex(sync->mutex);
return LV_RESULT_OK;
}
lv_result_t lv_thread_sync_delete(lv_thread_sync_t * sync)
{
SDL_DestroyMutex(sync->mutex);
SDL_DestroyCond(sync->cond);
return LV_RESULT_OK;
}
lv_result_t lv_thread_sync_signal_isr(lv_thread_sync_t * sync)
{
LV_UNUSED(sync);
return LV_RESULT_INVALID;
}
/**********************
* STATIC FUNCTIONS
**********************/
static int generic_callback(void * user_data)
{
lv_thread_t * thread = user_data;
thread->callback(thread->user_data);
return 0;
}
#endif /*LV_USE_OS == LV_OS_SDL2*/

55
src/osal/lv_sdl2.h Normal file
View File

@@ -0,0 +1,55 @@
/**
* @file lv_sdl2.h
*
*/
#ifndef LV_SDL2_H
#define LV_SDL2_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
#if LV_USE_OS == LV_OS_SDL2
#include <SDL2/SDL.h>
#include <stdbool.h>
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
typedef struct {
SDL_Thread * thread;
void (*callback)(void *);
void * user_data;
} lv_thread_t;
typedef SDL_mutex * lv_mutex_t;
typedef struct {
SDL_mutex * mutex;
SDL_cond * cond;
bool v;
} lv_thread_sync_t;
/**********************
* GLOBAL PROTOTYPES
**********************/
/**********************
* MACROS
**********************/
#endif /*LV_USE_OS == LV_OS_SDL2*/
#ifdef __cplusplus
} /*extern "C"*/
#endif
#endif /*LV_SDL2_H*/