Skip to content

Commit 6d8afc7

Browse files
committed
chore: migrate from SDL2 to SDL3
[no changelog]
1 parent 89f3301 commit 6d8afc7

18 files changed

Lines changed: 174 additions & 137 deletions

File tree

core/SConscript.bootloader_emu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ except OSError:
228228
print("libjpeg not installed, Emulator build is not possible")
229229

230230
try:
231-
env.ParseConfig('pkg-config --cflags --libs sdl2 SDL2_image')
231+
env.ParseConfig('pkg-config --cflags --libs sdl3 sdl3-image')
232232
except OSError:
233-
print("SDL2 not installed, Emulator build is not possible")
233+
print("SDL3 not installed, Emulator build is not possible")
234234

235235

236236
env.Replace(

core/SConscript.prodtest_emu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ except OSError:
243243
print("libjpeg not installed, Emulator build is not possible")
244244

245245
try:
246-
env.ParseConfig('pkg-config --cflags --libs sdl2 SDL2_image')
246+
env.ParseConfig('pkg-config --cflags --libs sdl3 sdl3-image')
247247
except OSError:
248-
print("SDL2 not installed, Emulator build is not possible")
248+
print("SDL3 not installed, Emulator build is not possible")
249249

250250

251251
env.Replace(

core/SConscript.unix

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,9 @@ except OSError:
562562
print("libjpeg not installed, Emulator build is not possible")
563563

564564
try:
565-
env.ParseConfig('pkg-config --cflags --libs sdl2 SDL2_image')
565+
env.ParseConfig('pkg-config --cflags --libs sdl3 sdl3-image')
566566
except OSError:
567-
print("SDL2 not installed, Emulator build is not possible")
567+
print("SDL3 not installed, Emulator build is not possible")
568568

569569
BYTECODE_OPTIMIZATION = {'0': '0', '1': '3'}[PYOPT]
570570

@@ -980,7 +980,8 @@ env.Depends(obj_program, qstr_generated)
980980
program = env.Command(
981981
target='trezor-emu-core',
982982
source=obj_program,
983-
action='$CC -o $TARGET $SOURCES $_LIBDIRFLAGS $_LIBFLAGS $LINKFLAGS', )
983+
action='$CC -o $TARGET $SOURCES $_LIBDIRFLAGS $_LIBFLAGS $LINKFLAGS',
984+
)
984985

985986
if CMAKELISTS != 0:
986987
env.Depends(program, cmake_gen)

core/embed/io/button/unix/button.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ void button_deinit(void) {
7979
static void button_sdl_event_filter(void* context, SDL_Event* sdl_event) {
8080
button_driver_t* drv = &g_button_driver;
8181

82-
if (sdl_event->type != SDL_KEYDOWN && sdl_event->type != SDL_KEYUP) {
82+
if (sdl_event->type != SDL_EVENT_KEY_DOWN &&
83+
sdl_event->type != SDL_EVENT_KEY_UP) {
8384
return;
8485
}
8586

@@ -89,7 +90,7 @@ static void button_sdl_event_filter(void* context, SDL_Event* sdl_event) {
8990

9091
button_t button;
9192

92-
switch (sdl_event->key.keysym.sym) {
93+
switch (sdl_event->key.key) {
9394
#ifdef BTN_LEFT_KEY
9495
case BTN_LEFT_KEY:
9596
button = BTN_LEFT;
@@ -109,7 +110,7 @@ static void button_sdl_event_filter(void* context, SDL_Event* sdl_event) {
109110
return;
110111
}
111112

112-
if (sdl_event->type == SDL_KEYDOWN) {
113+
if (sdl_event->type == SDL_EVENT_KEY_DOWN) {
113114
drv->state |= (1 << button);
114115
} else {
115116
drv->state &= ~(1 << button);

core/embed/io/display/unix/display_driver.c

Lines changed: 74 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
#include <sys/logging.h>
3131
#include <sys/systask.h>
3232

33-
#include <SDL2/SDL.h>
34-
#include <SDL2/SDL_blendmode.h>
35-
#include <SDL2/SDL_image.h>
36-
#include <SDL2/SDL_render.h>
33+
#include <SDL3/SDL.h>
34+
#include <SDL3/SDL_blendmode.h>
35+
#include <SDL3/SDL_render.h>
36+
#include <SDL3_image/SDL_image.h>
37+
#include <stdlib.h>
3738

3839
#include "profile.h"
3940

@@ -67,6 +68,14 @@ LOG_DECLARE(display_driver)
6768

6869
#endif
6970

71+
SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height,
72+
int depth, Uint32 Rmask, Uint32 Gmask,
73+
Uint32 Bmask, Uint32 Amask) {
74+
return SDL_CreateSurface(
75+
width, height,
76+
SDL_GetPixelFormatForMasks(depth, Rmask, Gmask, Bmask, Amask));
77+
}
78+
7079
typedef struct {
7180
// Set if the driver is initialized
7281
bool initialized;
@@ -109,7 +118,7 @@ bool display_init(display_content_mode_t mode) {
109118
return true;
110119
}
111120

112-
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
121+
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_HAPTIC | SDL_INIT_EVENTS)) {
113122
LOG_ERR("%s", SDL_GetError());
114123
error_shutdown("SDL_Init error");
115124
}
@@ -123,21 +132,19 @@ bool display_init(display_content_mode_t mode) {
123132
window_title_alloc = NULL;
124133
}
125134

126-
drv->window =
127-
SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED,
128-
SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT,
135+
drv->window = SDL_CreateWindow(window_title, WINDOW_WIDTH, WINDOW_HEIGHT,
129136
#ifdef TREZOR_EMULATOR_RASPI
130-
SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN
137+
SDL_WINDOW_FULLSCREEN
131138
#else
132-
SDL_WINDOW_SHOWN
139+
0 // windows are visible by default in SDL3
133140
#endif
134-
);
141+
);
135142
free(window_title_alloc);
136143
if (!drv->window) {
137144
LOG_ERR("%s", SDL_GetError());
138145
error_shutdown("SDL_CreateWindow error");
139146
}
140-
drv->renderer = SDL_CreateRenderer(drv->window, -1, SDL_RENDERER_SOFTWARE);
147+
drv->renderer = SDL_CreateRenderer(drv->window, NULL);
141148
if (!drv->renderer) {
142149
LOG_ERR("%s", SDL_GetError());
143150
SDL_DestroyWindow(drv->window);
@@ -162,31 +169,31 @@ bool display_init(display_content_mode_t mode) {
162169
#define CONCAT_LEN(name) CONCAT_LEN_HELPER(name)
163170
#ifdef BACKGROUND_FILE
164171
#include BACKGROUND_FILE
165-
drv->background = IMG_LoadTexture_RW(
172+
drv->background = IMG_LoadTexture_IO(
166173
drv->renderer,
167-
SDL_RWFromMem(BACKGROUND_NAME, CONCAT_LEN(BACKGROUND_NAME)), 0);
174+
SDL_IOFromMem(BACKGROUND_NAME, CONCAT_LEN(BACKGROUND_NAME)), true);
168175
#endif
169176
#ifdef FOREGROUND_FILE
170177
#include FOREGROUND_FILE
171-
drv->foreground = IMG_LoadTexture_RW(
178+
drv->foreground = IMG_LoadTexture_IO(
172179
drv->renderer,
173-
SDL_RWFromMem(FOREGROUND_NAME, CONCAT_LEN(FOREGROUND_NAME)), 0);
180+
SDL_IOFromMem(FOREGROUND_NAME, CONCAT_LEN(FOREGROUND_NAME)), true);
174181
if (drv->foreground) {
175182
SDL_SetTextureBlendMode(drv->foreground, SDL_BLENDMODE_BLEND);
176183
// check that foreground dimensions match the window size which is important
177184
// for cutouts
178-
int fw, fh;
179-
if (SDL_QueryTexture(drv->foreground, NULL, NULL, &fw, &fh) == 0) {
180-
if (fw != WINDOW_WIDTH || fh != WINDOW_HEIGHT) {
185+
float fw, fh;
186+
if (SDL_GetTextureSize(drv->foreground, &fw, &fh)) {
187+
if ((int)fw != WINDOW_WIDTH || (int)fh != WINDOW_HEIGHT) {
181188
LOG_ERR(
182189
"Foreground texture size (%dx%d) does not match window size "
183190
"(%dx%d)",
184-
fw, fh, WINDOW_WIDTH, WINDOW_HEIGHT);
191+
(int)fw, (int)fh, WINDOW_WIDTH, WINDOW_HEIGHT);
185192
error_shutdown("Foreground texture size mismatch");
186193
}
187194
} else {
188-
LOG_ERR("SDL_QueryTexture failed: %s", SDL_GetError());
189-
error_shutdown("SDL_QueryTexture error");
195+
LOG_ERR("SDL_GetTextureSize failed: %s", SDL_GetError());
196+
error_shutdown("SDL_GetTextureSize error");
190197
}
191198
}
192199
#endif
@@ -235,8 +242,8 @@ void display_deinit(display_content_mode_t mode) {
235242

236243
gfx_bitblt_deinit();
237244

238-
SDL_FreeSurface(drv->prev_saved);
239-
SDL_FreeSurface(drv->buffer);
245+
SDL_DestroySurface(drv->prev_saved);
246+
SDL_DestroySurface(drv->buffer);
240247
if (drv->background != NULL) {
241248
SDL_DestroyTexture(drv->background);
242249
}
@@ -416,22 +423,22 @@ void draw_rgb_led() {
416423
for (int y = -radius; y <= radius; y++) {
417424
for (int x = -radius; x <= radius; x++) {
418425
if (x * x + y * y <= radius * radius) {
419-
SDL_RenderDrawPoint(drv->renderer, center_x + x, center_y + y);
426+
SDL_RenderPoint(drv->renderer, center_x + x, center_y + y);
420427
}
421428
}
422429
}
423430
SDL_SetRenderDrawColor(drv->renderer, 0, 0, 0, 255);
424431
}
425432
#endif // USE_RGB_LED
426433

427-
static SDL_Rect screen_rect(void) {
434+
static SDL_FRect screen_rect(void) {
428435
display_driver_t *drv = &g_display_driver;
429436
if (drv->background || drv->foreground) {
430-
return (SDL_Rect){TOUCH_OFFSET_X, TOUCH_OFFSET_Y, DISPLAY_RESX,
431-
DISPLAY_RESY};
437+
return (SDL_FRect){TOUCH_OFFSET_X, TOUCH_OFFSET_Y, DISPLAY_RESX,
438+
DISPLAY_RESY};
432439
} else {
433-
return (SDL_Rect){EMULATOR_BORDER, EMULATOR_BORDER, DISPLAY_RESX,
434-
DISPLAY_RESY};
440+
return (SDL_FRect){EMULATOR_BORDER, EMULATOR_BORDER, DISPLAY_RESX,
441+
DISPLAY_RESY};
435442
}
436443
}
437444

@@ -447,8 +454,8 @@ static void display_refresh_internal(void) {
447454
#endif
448455

449456
if (drv->background) {
450-
const SDL_Rect r = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
451-
SDL_RenderCopy(drv->renderer, drv->background, NULL, &r);
457+
const SDL_FRect r = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
458+
SDL_RenderTexture(drv->renderer, drv->background, NULL, &r);
452459
} else {
453460
SDL_RenderClear(drv->renderer);
454461
}
@@ -458,13 +465,13 @@ static void display_refresh_internal(void) {
458465
#define BACKLIGHT_NORMAL 150
459466
SDL_SetTextureAlphaMod(
460467
drv->texture, MIN(255, 255 * drv->backlight_level / BACKLIGHT_NORMAL));
461-
const SDL_Rect r = screen_rect();
462-
SDL_RenderCopyEx(drv->renderer, drv->texture, NULL, &r,
463-
drv->orientation_angle, NULL, 0);
468+
const SDL_FRect r = screen_rect();
469+
SDL_RenderTextureRotated(drv->renderer, drv->texture, NULL, &r,
470+
drv->orientation_angle, NULL, 0);
464471

465472
if (drv->foreground) {
466-
const SDL_Rect fr = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
467-
SDL_RenderCopy(drv->renderer, drv->foreground, NULL, &fr);
473+
const SDL_FRect fr = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
474+
SDL_RenderTexture(drv->renderer, drv->foreground, NULL, &fr);
468475
}
469476

470477
#ifdef USE_RGB_LED
@@ -597,19 +604,20 @@ void display_save(const char *prefix) {
597604
static char filename[256];
598605
// take a cropped view of the screen contents
599606
const SDL_Rect rect = {0, 0, DISPLAY_RESX, DISPLAY_RESY};
600-
SDL_Surface *crop = SDL_CreateRGBSurface(
601-
drv->buffer->flags, rect.w, rect.h, drv->buffer->format->BitsPerPixel,
602-
drv->buffer->format->Rmask, drv->buffer->format->Gmask,
603-
drv->buffer->format->Bmask, drv->buffer->format->Amask);
607+
SDL_Surface *crop = SDL_CreateSurface(rect.w, rect.h, drv->buffer->format);
608+
if (crop == NULL) {
609+
LOG_ERR("SDL_CreateSurface failed: %s", SDL_GetError());
610+
return;
611+
}
604612
SDL_BlitSurface(drv->buffer, &rect, crop, NULL);
605613
// compare with previous screen, skip if equal
606614
if (drv->prev_saved != NULL) {
607615
if (memcmp(drv->prev_saved->pixels, crop->pixels, crop->pitch * crop->h) ==
608616
0) {
609-
SDL_FreeSurface(crop);
617+
SDL_DestroySurface(crop);
610618
return;
611619
}
612-
SDL_FreeSurface(drv->prev_saved);
620+
SDL_DestroySurface(drv->prev_saved);
613621
}
614622
// save to png
615623
snprintf(filename, sizeof(filename), "%s%08d.png", prefix, count++);
@@ -624,7 +632,7 @@ void display_clear_save(void) {
624632
return;
625633
}
626634

627-
SDL_FreeSurface(drv->prev_saved);
635+
SDL_DestroySurface(drv->prev_saved);
628636
drv->prev_saved = NULL;
629637
}
630638

@@ -636,7 +644,7 @@ static void display_draw_suspend_overlay_internal(void) {
636644
return;
637645
}
638646

639-
SDL_Rect screen = screen_rect();
647+
SDL_FRect screen = screen_rect();
640648
// create a blue texture
641649
SDL_Texture *overlay =
642650
SDL_CreateTexture(drv->renderer, SDL_PIXELFORMAT_RGBA8888,
@@ -649,19 +657,32 @@ static void display_draw_suspend_overlay_internal(void) {
649657
SDL_RenderClear(drv->renderer);
650658

651659
// draw the suspend overlay png in the middle of the texture
652-
SDL_Texture *suspend_text = IMG_LoadTexture_RW(
660+
SDL_Texture *suspend_text = IMG_LoadTexture_IO(
653661
drv->renderer,
654-
SDL_RWFromMem(_suspend_overlay_text_data, _suspend_overlay_text_len), 0);
655-
int text_width, text_height;
656-
SDL_QueryTexture(suspend_text, NULL, NULL, &text_width, &text_height);
657-
SDL_Rect middle = {(screen.w - text_width) / 2, (screen.h - text_height) / 2,
658-
text_width, text_height};
659-
SDL_RenderCopy(drv->renderer, suspend_text, NULL, &middle);
662+
SDL_IOFromMem(_suspend_overlay_text_data, _suspend_overlay_text_len),
663+
true);
664+
if (suspend_text == NULL) {
665+
LOG_ERR("Failed to load suspend overlay texture: %s", SDL_GetError());
666+
SDL_DestroyTexture(overlay);
667+
SDL_SetRenderTarget(drv->renderer, NULL);
668+
return;
669+
}
670+
float text_width = 0.0f, text_height = 0.0f;
671+
if (!SDL_GetTextureSize(suspend_text, &text_width, &text_height)) {
672+
LOG_ERR("SDL_GetTextureSize failed: %s", SDL_GetError());
673+
SDL_DestroyTexture(suspend_text);
674+
SDL_DestroyTexture(overlay);
675+
SDL_SetRenderTarget(drv->renderer, NULL);
676+
return;
677+
}
678+
SDL_FRect middle = {(screen.w - text_width) / 2, (screen.h - text_height) / 2,
679+
text_width, text_height};
680+
SDL_RenderTexture(drv->renderer, suspend_text, NULL, &middle);
660681
SDL_RenderPresent(drv->renderer);
661682

662683
// render to the screen
663684
SDL_SetRenderTarget(drv->renderer, NULL);
664-
SDL_RenderCopy(drv->renderer, overlay, NULL, &screen);
685+
SDL_RenderTexture(drv->renderer, overlay, NULL, &screen);
665686
SDL_RenderPresent(drv->renderer);
666687

667688
// cleanup

core/embed/io/power_manager/unix/power_manager.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
#include <io/power_manager.h>
2424
#include <io/unix/sdl_display.h>
2525

26-
#include <SDL.h>
27-
#include <SDL2/SDL_events.h>
26+
#include <SDL3/SDL.h>
27+
#include <stdlib.h>
28+
#include "SDL3/SDL_events.h"
2829

2930
#include "../power_manager_poll.h"
3031

@@ -76,11 +77,12 @@ pm_status_t pm_suspend(wakeup_flags_t* wakeup_reason) {
7677

7778
SDL_Event event;
7879
while (SDL_WaitEvent(&event)) {
79-
if (event.type == SDL_QUIT) {
80+
if (event.type == SDL_EVENT_QUIT) {
8081
exit(1);
8182
}
82-
if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP ||
83-
event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP) {
83+
if (event.type == SDL_EVENT_KEY_DOWN || event.type == SDL_EVENT_KEY_UP ||
84+
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN ||
85+
event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
8486
*wakeup_reason = WAKEUP_FLAG_BUTTON;
8587
break;
8688
}

0 commit comments

Comments
 (0)