Improve startup time when show_touches is enabled

Enabling "show touches" involves the execution of an adb command, which
takes some time.

In order to parallelize, execute the command as soon as possible, but
reap the process only once everything is initialized.
This commit is contained in:
Romain Vimont 2018-03-25 16:20:34 +02:00
parent dd2a5c1ecf
commit 57eaf05289

View file

@ -106,13 +106,17 @@ static void event_loop(void) {
} }
} }
static SDL_bool set_show_touches_enabled(const char *serial, SDL_bool enabled) { static process_t set_show_touches_enabled(const char *serial, SDL_bool enabled) {
const char *value = enabled ? "1" : "0"; const char *value = enabled ? "1" : "0";
const char *const adb_cmd[] = { const char *const adb_cmd[] = {
"shell", "settings", "put", "system", "show_touches", value "shell", "settings", "put", "system", "show_touches", value
}; };
process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
return process_check_success(proc, "show_touches"); }
static void wait_show_touches(process_t process) {
// reap the process, ignore the result
process_check_success(process, "show_touches");
} }
SDL_bool scrcpy(const struct scrcpy_options *options) { SDL_bool scrcpy(const struct scrcpy_options *options) {
@ -121,6 +125,14 @@ SDL_bool scrcpy(const struct scrcpy_options *options) {
return SDL_FALSE; return SDL_FALSE;
} }
process_t proc_show_touches;
SDL_bool show_touches_waited;
if (options->show_touches) {
LOGI("Enable show_touches");
proc_show_touches = set_show_touches_enabled(options->serial, SDL_TRUE);
show_touches_waited = SDL_FALSE;
}
SDL_bool ret = SDL_TRUE; SDL_bool ret = SDL_TRUE;
if (!SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1")) { if (!SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1")) {
@ -183,8 +195,8 @@ SDL_bool scrcpy(const struct scrcpy_options *options) {
} }
if (options->show_touches) { if (options->show_touches) {
LOGI("Enable show_touches"); wait_show_touches(proc_show_touches);
set_show_touches_enabled(options->serial, SDL_TRUE); show_touches_waited = SDL_TRUE;
} }
event_loop(); event_loop();
@ -192,10 +204,6 @@ SDL_bool scrcpy(const struct scrcpy_options *options) {
screen_destroy(&screen); screen_destroy(&screen);
if (options->show_touches) {
LOGI("Disable show_touches");
set_show_touches_enabled(options->serial, SDL_FALSE);
}
finally_stop_and_join_controller: finally_stop_and_join_controller:
controller_stop(&controller); controller_stop(&controller);
controller_join(&controller); controller_join(&controller);
@ -209,6 +217,16 @@ finally_stop_decoder:
finally_destroy_frames: finally_destroy_frames:
frames_destroy(&frames); frames_destroy(&frames);
finally_destroy_server: finally_destroy_server:
if (options->show_touches) {
if (!show_touches_waited) {
// wait the process which enabled "show touches"
wait_show_touches(proc_show_touches);
}
LOGI("Disable show_touches");
proc_show_touches = set_show_touches_enabled(options->serial, SDL_FALSE);
wait_show_touches(proc_show_touches);
}
server_destroy(&server); server_destroy(&server);
return ret; return ret;