Display and control your Android device
Improve startup time
Romain Vimont90a46b4c4563
diff --git a/app/src/scrcpy.c b/app/src/scrcpy.cindex 6a354290..29ce87b5 100644--- a/app/src/scrcpy.c+++ b/app/src/scrcpy.c@@ -57,6 +57,11 @@ static void event_loop(void) {SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "User requested to quit");return;case EVENT_NEW_FRAME:+ if (!screen.has_frame) {+ screen.has_frame = SDL_TRUE;+ // this is the very first frame, show the window+ screen_show_window(&screen);+ }if (!screen_update_frame(&screen, &frames)) {return;}@@ -101,6 +106,11 @@ SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 bSDL_bool ret = SDL_TRUE;+ if (!sdl_init_and_configure()) {+ ret = SDL_FALSE;+ goto finally_destroy_server;+ }+// to reduce startup time, we could be tempted to init other stuff before blocking here// but we should not block after SDL_Init since it handles the signals (Ctrl+C) in its// event loop: blocking could lead to deadlock@@ -149,11 +159,6 @@ SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 bgoto finally_destroy_controller;}- if (!sdl_init_and_configure()) {- ret = SDL_FALSE;- goto finally_stop_and_join_controller;- }-if (!screen_init_rendering(&screen, device_name, frame_size)) {ret = SDL_FALSE;goto finally_stop_and_join_controller;diff --git a/app/src/screen.c b/app/src/screen.cindex b56f8861..613bdabe 100644--- a/app/src/screen.c+++ b/app/src/screen.c@@ -137,7 +137,7 @@ SDL_bool screen_init_rendering(struct screen *screen, const char *device_name, sstruct size window_size = get_initial_optimal_size(frame_size);screen->window = SDL_CreateWindow(device_name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,- window_size.width, window_size.height, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);+ window_size.width, window_size.height, SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE);if (!screen->window) {SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "Could not create window: %s", SDL_GetError());return SDL_FALSE;@@ -178,6 +178,10 @@ SDL_bool screen_init_rendering(struct screen *screen, const char *device_name, sreturn SDL_TRUE;}+void screen_show_window(struct screen *screen) {+ SDL_ShowWindow(screen->window);+}+void screen_destroy(struct screen *screen) {if (screen->texture) {SDL_DestroyTexture(screen->texture);@@ -231,7 +235,6 @@ static SDL_bool prepare_for_frame(struct screen *screen, struct size new_frame_s// write the frame into the texturestatic void update_texture(struct screen *screen, const AVFrame *frame) {- screen->texture_initialized = SDL_TRUE;SDL_UpdateYUVTexture(screen->texture, NULL,frame->data[0], frame->linesize[0],frame->data[1], frame->linesize[1],@@ -255,9 +258,7 @@ SDL_bool screen_update_frame(struct screen *screen, struct frames *frames) {void screen_render(struct screen *screen) {SDL_RenderClear(screen->renderer);- if (screen->texture_initialized) {- SDL_RenderCopy(screen->renderer, screen->texture, NULL, NULL);- }+ SDL_RenderCopy(screen->renderer, screen->texture, NULL, NULL);SDL_RenderPresent(screen->renderer);}diff --git a/app/src/screen.h b/app/src/screen.hindex 1dc31904..13103eaa 100644--- a/app/src/screen.h+++ b/app/src/screen.h@@ -14,24 +14,24 @@ struct screen {struct size frame_size;//used only in fullscreen mode to know the windowed window sizestruct size windowed_window_size;- SDL_bool texture_initialized;+ SDL_bool has_frame;SDL_bool fullscreen;};-#define SCREEN_INITIALIZER { \- .window = NULL, \- .renderer = NULL, \- .texture = NULL, \- .frame_size = { \- .width = 0, \- .height = 0, \- }, \- .windowed_window_size = { \- .width = 0, \- .height = 0, \- }, \- .texture_initialized = SDL_FALSE, \- .fullscreen = SDL_FALSE, \+#define SCREEN_INITIALIZER { \+ .window = NULL, \+ .renderer = NULL, \+ .texture = NULL, \+ .frame_size = { \+ .width = 0, \+ .height = 0, \+ }, \+ .windowed_window_size = { \+ .width = 0, \+ .height = 0, \+ }, \+ .has_frame = SDL_FALSE, \+ .fullscreen = SDL_FALSE, \}// init SDL and set appropriate hints@@ -40,11 +40,14 @@ SDL_bool sdl_init_and_configure(void);// initialize default valuesvoid screen_init(struct screen *screen);-// initialize screen, create window, renderer and texture+// initialize screen, create window, renderer and texture (window is hidden)SDL_bool screen_init_rendering(struct screen *screen,const char *device_name,struct size frame_size);+// show the window+void screen_show_window(struct screen *screen);+// destroy window, renderer and texture (if any)void screen_destroy(struct screen *screen);