Просмотр исходного кода

update: sdl2, 从ARGB8888纹理更新为RGB565,屏幕显示更加贴近真机效果

zengeshuai 1 месяц назад
Родитель
Сommit
ec4282dedc
2 измененных файлов с 12 добавлено и 20 удалено
  1. 9 15
      components/ui/sdl2/luat_lcd_sdl2.c
  2. 3 5
      components/ui/sdl2/luat_sdl2.c

+ 9 - 15
components/ui/sdl2/luat_lcd_sdl2.c

@@ -9,7 +9,7 @@
 #define LUAT_LOG_TAG "ui_sdl2"
 #include "luat_log.h"
 
-static uint32_t* fb;
+static luat_color_t* fb;
 
 static inline uint32_t luat_color_565to8888(luat_color_t color);
 
@@ -19,7 +19,7 @@ static int sdl2_init(luat_lcd_conf_t* conf) {
         .height = conf->h
     };
     luat_sdl2_init(&sdl2_conf);
-    size_t fb_size = sizeof(uint32_t) * conf->w * conf->h;
+    size_t fb_size = sizeof(luat_color_t) * conf->w * conf->h;
     fb = luat_heap_opt_malloc(LUAT_HEAP_PSRAM, fb_size);
     if (fb == NULL) {
         fb = luat_heap_opt_malloc(LUAT_HEAP_SRAM, fb_size);
@@ -34,7 +34,8 @@ static int sdl2_init(luat_lcd_conf_t* conf) {
             return -1;
         }
     }
-    LLOGD("sdl2_init conf->buff %p, conf->opts %p fb %p", conf->buff, conf->opts, fb);
+    LLOGD("sdl2_init w %d h %d fb_size %u conf->buff %p conf->opts %p fb %p",
+          conf->w, conf->h, (unsigned int)fb_size, conf->buff, conf->opts, fb);
     luat_lcd_clear(conf, LCD_WHITE);
     // printf("ARGB8888 0xFFFF %08X\n", luat_color_565to8888(0xFFFF));
     // printf("ARGB8888 0X001F %08X\n", luat_color_565to8888(0X001F));
@@ -94,14 +95,7 @@ int luat_lcd_flush(luat_lcd_conf_t* conf) {
     size_t height = y_max - y_min + 1;
 
     luat_color_t* src = conf->buff + y_min * conf->w;
-    uint32_t* tmp = fb;
-    for (size_t row = 0; row < height; row++) {
-        for (size_t col = 0; col < width; col++) {
-            tmp[row * width + col] = luat_color_565to8888(src[col]);
-        }
-        src += conf->w;
-    }
-    luat_sdl2_draw(0, y_min, conf->w - 1, y_max, fb);
+    luat_sdl2_draw(0, y_min, conf->w - 1, y_max, src);
     luat_sdl2_flush();
 
     conf->flush_y_max = 0;
@@ -155,18 +149,18 @@ int luat_lcd_draw(luat_lcd_conf_t* conf, int16_t x1, int16_t y1, int16_t x2, int
     size_t rw = x2 - x1 + 1;
     size_t rh = y2 - y1 + 1;
 
-    uint32_t *tmp = fb;
+    luat_color_t *tmp = fb;
     for (size_t i = 0; i < rh; i++)
     {
         for (size_t j = 0; j < rw; j++)
         {
-            // 输入为 RGB565,SDL 纹理为 ARGB8888,这里做明确转换
-            *tmp = luat_color_565to8888(*color_p);
+            // 直接拷贝 RGB565 数据
+            *tmp = *color_p;
             tmp ++;
             color_p ++;
         }
     }
-    
+
     luat_sdl2_draw(x1, y1, x2, y2, fb);
     return 0;
 }

+ 3 - 5
components/ui/sdl2/luat_sdl2.c

@@ -9,8 +9,6 @@
 static SDL_Window *window = NULL;
 static SDL_Renderer *renderer = NULL;
 static SDL_Texture *framebuffer = NULL;
-
-static uint32_t* fb;
 static luat_sdl2_conf_t sdl_conf;
 
 static void luat_sdl2_pump_events(void) {
@@ -41,11 +39,10 @@ int luat_sdl2_init(luat_sdl2_conf_t *conf) {
 
     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
     framebuffer = SDL_CreateTexture(renderer,
-                                    SDL_PIXELFORMAT_ARGB8888,
+                                    SDL_PIXELFORMAT_RGB565,
                                     SDL_TEXTUREACCESS_STREAMING,
                                     conf->width,
                                     conf->height);
-    // fb = luat_heap_malloc(sizeof(uint32_t) * conf->width * conf->height);
     luat_sdl2_pump_events();
     return 0;
 }
@@ -70,7 +67,8 @@ void luat_sdl2_draw(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint32_t* da
     r.w = x2 - x1 + 1;
     r.h = y2 - y1 + 1;
 
-    SDL_UpdateTexture(framebuffer, &r, data, r.w * 4);
+    // RGB565: 2 bytes per pixel
+    SDL_UpdateTexture(framebuffer, &r, data, r.w * 2);
 }
 
 void luat_sdl2_flush(void) {