mirror of
https://github.com/Genymobile/scrcpy.git
synced 2026-02-19 04:44:27 +01:00
Migrate from SDL2 to SDL3
Refs <https://wiki.libsdl.org/SDL3/README-migration>
This commit is contained in:
@@ -50,13 +50,13 @@ log_level_sdl_to_sc(SDL_LogPriority priority) {
|
||||
void
|
||||
sc_set_log_level(enum sc_log_level level) {
|
||||
SDL_LogPriority sdl_log = log_level_sc_to_sdl(level);
|
||||
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, sdl_log);
|
||||
SDL_LogSetPriority(SDL_LOG_CATEGORY_CUSTOM, sdl_log);
|
||||
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, sdl_log);
|
||||
SDL_SetLogPriority(SDL_LOG_CATEGORY_CUSTOM, sdl_log);
|
||||
}
|
||||
|
||||
enum sc_log_level
|
||||
sc_get_log_level(void) {
|
||||
SDL_LogPriority sdl_log = SDL_LogGetPriority(SDL_LOG_CATEGORY_APPLICATION);
|
||||
SDL_LogPriority sdl_log = SDL_GetLogPriority(SDL_LOG_CATEGORY_APPLICATION);
|
||||
return log_level_sdl_to_sc(sdl_log);
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ sc_av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
|
||||
free(local_fmt);
|
||||
}
|
||||
|
||||
static const char *const sc_sdl_log_priority_names[SDL_NUM_LOG_PRIORITIES] = {
|
||||
static const char *const sc_sdl_log_priority_names[SDL_LOG_PRIORITY_COUNT] = {
|
||||
[SDL_LOG_PRIORITY_VERBOSE] = "VERBOSE",
|
||||
[SDL_LOG_PRIORITY_DEBUG] = "DEBUG",
|
||||
[SDL_LOG_PRIORITY_INFO] = "INFO",
|
||||
@@ -144,14 +144,14 @@ sc_sdl_log_print(void *userdata, int category, SDL_LogPriority priority,
|
||||
(void) category;
|
||||
|
||||
FILE *out = priority < SDL_LOG_PRIORITY_WARN ? stdout : stderr;
|
||||
assert(priority < SDL_NUM_LOG_PRIORITIES);
|
||||
assert(priority < SDL_LOG_PRIORITY_COUNT);
|
||||
const char *prio_name = sc_sdl_log_priority_names[priority];
|
||||
fprintf(out, "%s: %s\n", prio_name, message);
|
||||
}
|
||||
|
||||
void
|
||||
sc_log_configure(void) {
|
||||
SDL_LogSetOutputFunction(sc_sdl_log_print, NULL);
|
||||
SDL_SetLogOutputFunction(sc_sdl_log_print, NULL);
|
||||
// Redirect FFmpeg logs to SDL logs
|
||||
av_log_set_callback(sc_av_log_callback);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <SDL2/SDL_log.h>
|
||||
#include <SDL3/SDL_log.h>
|
||||
|
||||
#include "options.h"
|
||||
|
||||
|
||||
107
app/src/util/sdl.c
Normal file
107
app/src/util/sdl.c
Normal file
@@ -0,0 +1,107 @@
|
||||
#include "sdl.h"
|
||||
|
||||
SDL_Window *
|
||||
sc_sdl_create_window(const char *title, int64_t x, int64_t y, int64_t width,
|
||||
int64_t height, int64_t flags) {
|
||||
SDL_Window *window = NULL;
|
||||
|
||||
SDL_PropertiesID props = SDL_CreateProperties();
|
||||
if (!props) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool ok =
|
||||
SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING,
|
||||
title);
|
||||
ok &= SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, x);
|
||||
ok &= SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, y);
|
||||
ok &= SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER,
|
||||
width);
|
||||
ok &= SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER,
|
||||
height);
|
||||
ok &= SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_FLAGS_NUMBER,
|
||||
flags);
|
||||
|
||||
if (!ok) {
|
||||
SDL_DestroyProperties(props);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
window = SDL_CreateWindowWithProperties(props);
|
||||
SDL_DestroyProperties(props);
|
||||
return window;
|
||||
}
|
||||
|
||||
struct sc_size
|
||||
sc_sdl_get_window_size(SDL_Window *window) {
|
||||
int width;
|
||||
int height;
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
|
||||
struct sc_size size = {
|
||||
.width = width,
|
||||
.height = height,
|
||||
};
|
||||
return size;
|
||||
}
|
||||
|
||||
struct sc_size
|
||||
sc_sdl_get_window_size_in_pixels(SDL_Window *window) {
|
||||
int width;
|
||||
int height;
|
||||
SDL_GetWindowSizeInPixels(window, &width, &height);
|
||||
|
||||
struct sc_size size = {
|
||||
.width = width,
|
||||
.height = height,
|
||||
};
|
||||
return size;
|
||||
}
|
||||
|
||||
void
|
||||
sc_sdl_set_window_size(SDL_Window *window, struct sc_size size) {
|
||||
SDL_SetWindowSize(window, size.width, size.height);
|
||||
}
|
||||
|
||||
struct sc_point
|
||||
sc_sdl_get_window_position(SDL_Window *window) {
|
||||
int x;
|
||||
int y;
|
||||
SDL_GetWindowPosition(window, &x, &y);
|
||||
|
||||
struct sc_point point = {
|
||||
.x = x,
|
||||
.y = y,
|
||||
};
|
||||
return point;
|
||||
}
|
||||
|
||||
void
|
||||
sc_sdl_set_window_position(SDL_Window *window, struct sc_point point) {
|
||||
SDL_SetWindowPosition(window, point.x, point.y);
|
||||
}
|
||||
|
||||
void
|
||||
sc_sdl_show_window(SDL_Window *window) {
|
||||
SDL_ShowWindow(window);
|
||||
}
|
||||
|
||||
void
|
||||
sc_sdl_hide_window(SDL_Window *window) {
|
||||
SDL_HideWindow(window);
|
||||
}
|
||||
|
||||
void
|
||||
sc_sdl_restore_window(SDL_Window *window) {
|
||||
SDL_RestoreWindow(window);
|
||||
}
|
||||
|
||||
bool
|
||||
sc_sdl_render_clear(SDL_Renderer *renderer) {
|
||||
return SDL_RenderClear(renderer);
|
||||
}
|
||||
|
||||
void
|
||||
sc_sdl_render_present(SDL_Renderer *renderer) {
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
46
app/src/util/sdl.h
Normal file
46
app/src/util/sdl.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef SC_SDL_H
|
||||
#define SC_SDL_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <SDL3/SDL_render.h>
|
||||
#include <SDL3/SDL_video.h>
|
||||
|
||||
#include "coords.h"
|
||||
|
||||
SDL_Window *
|
||||
sc_sdl_create_window(const char *title, int64_t x, int64_t y, int64_t width,
|
||||
int64_t height, int64_t flags);
|
||||
|
||||
struct sc_size
|
||||
sc_sdl_get_window_size(SDL_Window *window);
|
||||
|
||||
struct sc_size
|
||||
sc_sdl_get_window_size_in_pixels(SDL_Window *window);
|
||||
|
||||
void
|
||||
sc_sdl_set_window_size(SDL_Window *window, struct sc_size size);
|
||||
|
||||
struct sc_point
|
||||
sc_sdl_get_window_position(SDL_Window *window);
|
||||
|
||||
void
|
||||
sc_sdl_set_window_position(SDL_Window *window, struct sc_point point);
|
||||
|
||||
void
|
||||
sc_sdl_show_window(SDL_Window *window);
|
||||
|
||||
void
|
||||
sc_sdl_hide_window(SDL_Window *window);
|
||||
|
||||
void
|
||||
sc_sdl_restore_window(SDL_Window *window);
|
||||
|
||||
bool
|
||||
sc_sdl_render_clear(SDL_Renderer *renderer);
|
||||
|
||||
void
|
||||
sc_sdl_render_present(SDL_Renderer *renderer);
|
||||
|
||||
#endif
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <SDL2/SDL_thread.h>
|
||||
#include <SDL3/SDL_mutex.h>
|
||||
|
||||
#include "util/log.h"
|
||||
|
||||
@@ -31,11 +31,7 @@ static SDL_ThreadPriority
|
||||
to_sdl_thread_priority(enum sc_thread_priority priority) {
|
||||
switch (priority) {
|
||||
case SC_THREAD_PRIORITY_TIME_CRITICAL:
|
||||
#ifdef SCRCPY_SDL_HAS_THREAD_PRIORITY_TIME_CRITICAL
|
||||
return SDL_THREAD_PRIORITY_TIME_CRITICAL;
|
||||
#else
|
||||
// fall through
|
||||
#endif
|
||||
case SC_THREAD_PRIORITY_HIGH:
|
||||
return SDL_THREAD_PRIORITY_HIGH;
|
||||
case SC_THREAD_PRIORITY_NORMAL:
|
||||
@@ -51,8 +47,8 @@ to_sdl_thread_priority(enum sc_thread_priority priority) {
|
||||
bool
|
||||
sc_thread_set_priority(enum sc_thread_priority priority) {
|
||||
SDL_ThreadPriority sdl_priority = to_sdl_thread_priority(priority);
|
||||
int r = SDL_SetThreadPriority(sdl_priority);
|
||||
if (r) {
|
||||
bool ok = SDL_SetCurrentThreadPriority(sdl_priority);
|
||||
if (!ok) {
|
||||
LOGD("Could not set thread priority: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
@@ -67,7 +63,7 @@ sc_thread_join(sc_thread *thread, int *status) {
|
||||
|
||||
bool
|
||||
sc_mutex_init(sc_mutex *mutex) {
|
||||
SDL_mutex *sdl_mutex = SDL_CreateMutex();
|
||||
SDL_Mutex *sdl_mutex = SDL_CreateMutex();
|
||||
if (!sdl_mutex) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
@@ -89,40 +85,25 @@ void
|
||||
sc_mutex_lock(sc_mutex *mutex) {
|
||||
// SDL mutexes are recursive, but we don't want to use recursive mutexes
|
||||
assert(!sc_mutex_held(mutex));
|
||||
int r = SDL_LockMutex(mutex->mutex);
|
||||
SDL_LockMutex(mutex->mutex);
|
||||
#ifndef NDEBUG
|
||||
if (r) {
|
||||
LOGE("Could not lock mutex: %s", SDL_GetError());
|
||||
abort();
|
||||
}
|
||||
|
||||
atomic_store_explicit(&mutex->locker, sc_thread_get_id(),
|
||||
memory_order_relaxed);
|
||||
#else
|
||||
(void) r;
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
sc_mutex_unlock(sc_mutex *mutex) {
|
||||
#ifndef NDEBUG
|
||||
assert(sc_mutex_held(mutex));
|
||||
#ifndef NDEBUG
|
||||
atomic_store_explicit(&mutex->locker, 0, memory_order_relaxed);
|
||||
#endif
|
||||
int r = SDL_UnlockMutex(mutex->mutex);
|
||||
#ifndef NDEBUG
|
||||
if (r) {
|
||||
LOGE("Could not lock mutex: %s", SDL_GetError());
|
||||
abort();
|
||||
}
|
||||
#else
|
||||
(void) r;
|
||||
#endif
|
||||
SDL_UnlockMutex(mutex->mutex);
|
||||
}
|
||||
|
||||
sc_thread_id
|
||||
sc_thread_get_id(void) {
|
||||
return SDL_ThreadID();
|
||||
return SDL_GetCurrentThreadID();
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
@@ -136,7 +117,7 @@ sc_mutex_held(struct sc_mutex *mutex) {
|
||||
|
||||
bool
|
||||
sc_cond_init(sc_cond *cond) {
|
||||
SDL_cond *sdl_cond = SDL_CreateCond();
|
||||
SDL_Condition *sdl_cond = SDL_CreateCondition();
|
||||
if (!sdl_cond) {
|
||||
LOG_OOM();
|
||||
return false;
|
||||
@@ -148,22 +129,15 @@ sc_cond_init(sc_cond *cond) {
|
||||
|
||||
void
|
||||
sc_cond_destroy(sc_cond *cond) {
|
||||
SDL_DestroyCond(cond->cond);
|
||||
SDL_DestroyCondition(cond->cond);
|
||||
}
|
||||
|
||||
void
|
||||
sc_cond_wait(sc_cond *cond, sc_mutex *mutex) {
|
||||
int r = SDL_CondWait(cond->cond, mutex->mutex);
|
||||
SDL_WaitCondition(cond->cond, mutex->mutex);
|
||||
#ifndef NDEBUG
|
||||
if (r) {
|
||||
LOGE("Could not wait on condition: %s", SDL_GetError());
|
||||
abort();
|
||||
}
|
||||
|
||||
atomic_store_explicit(&mutex->locker, sc_thread_get_id(),
|
||||
memory_order_relaxed);
|
||||
#else
|
||||
(void) r;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -177,44 +151,22 @@ sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick deadline) {
|
||||
// Round up to the next millisecond to guarantee that the deadline is
|
||||
// reached when returning due to timeout
|
||||
uint32_t ms = SC_TICK_TO_MS(deadline - now + SC_TICK_FROM_MS(1) - 1);
|
||||
int r = SDL_CondWaitTimeout(cond->cond, mutex->mutex, ms);
|
||||
bool signaled = SDL_WaitConditionTimeout(cond->cond, mutex->mutex, ms);
|
||||
#ifndef NDEBUG
|
||||
if (r < 0) {
|
||||
LOGE("Could not wait on condition with timeout: %s", SDL_GetError());
|
||||
abort();
|
||||
}
|
||||
|
||||
atomic_store_explicit(&mutex->locker, sc_thread_get_id(),
|
||||
memory_order_relaxed);
|
||||
#endif
|
||||
assert(r == 0 || r == SDL_MUTEX_TIMEDOUT);
|
||||
// The deadline is reached on timeout
|
||||
assert(r != SDL_MUTEX_TIMEDOUT || sc_tick_now() >= deadline);
|
||||
return r == 0;
|
||||
assert(signaled || sc_tick_now() >= deadline);
|
||||
return signaled;
|
||||
}
|
||||
|
||||
void
|
||||
sc_cond_signal(sc_cond *cond) {
|
||||
int r = SDL_CondSignal(cond->cond);
|
||||
#ifndef NDEBUG
|
||||
if (r) {
|
||||
LOGE("Could not signal a condition: %s", SDL_GetError());
|
||||
abort();
|
||||
}
|
||||
#else
|
||||
(void) r;
|
||||
#endif
|
||||
SDL_SignalCondition(cond->cond);
|
||||
}
|
||||
|
||||
void
|
||||
sc_cond_broadcast(sc_cond *cond) {
|
||||
int r = SDL_CondBroadcast(cond->cond);
|
||||
#ifndef NDEBUG
|
||||
if (r) {
|
||||
LOGE("Could not broadcast a condition: %s", SDL_GetError());
|
||||
abort();
|
||||
}
|
||||
#else
|
||||
(void) r;
|
||||
#endif
|
||||
SDL_BroadcastCondition(cond->cond);
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
|
||||
/* Forward declarations */
|
||||
typedef struct SDL_Thread SDL_Thread;
|
||||
typedef struct SDL_mutex SDL_mutex;
|
||||
typedef struct SDL_cond SDL_cond;
|
||||
typedef struct SDL_Mutex SDL_Mutex;
|
||||
typedef struct SDL_Condition SDL_Condition;
|
||||
|
||||
typedef int sc_thread_fn(void *);
|
||||
typedef unsigned sc_thread_id;
|
||||
@@ -29,14 +29,14 @@ enum sc_thread_priority {
|
||||
};
|
||||
|
||||
typedef struct sc_mutex {
|
||||
SDL_mutex *mutex;
|
||||
SDL_Mutex *mutex;
|
||||
#ifndef NDEBUG
|
||||
sc_atomic_thread_id locker;
|
||||
#endif
|
||||
} sc_mutex;
|
||||
|
||||
typedef struct sc_cond {
|
||||
SDL_cond *cond;
|
||||
SDL_Condition *cond;
|
||||
} sc_cond;
|
||||
|
||||
extern sc_thread_id SC_MAIN_THREAD_ID;
|
||||
|
||||
Reference in New Issue
Block a user