Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Improve responsiveness on underpowered Android devices #42220

Merged
merged 4 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/os/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ class Input : public Object {
virtual int get_joy_axis_index_from_string(String p_axis) = 0;

virtual void parse_input_event(const Ref<InputEvent> &p_event) = 0;
virtual void accumulate_input_event(const Ref<InputEvent> &p_event) = 0;
virtual void flush_accumulated_events() = 0;
virtual void flush_buffered_events() = 0;
virtual bool is_using_input_buffering() = 0;
virtual void set_use_input_buffering(bool p_enable) = 0;
virtual void set_use_accumulated_input(bool p_enable) = 0;

Input();
Expand Down
16 changes: 16 additions & 0 deletions core/os/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,22 @@ String InputEventScreenDrag::as_text() const {
return "InputEventScreenDrag : index=" + itos(index) + ", position=(" + String(get_position()) + "), relative=(" + String(get_relative()) + "), speed=(" + String(get_speed()) + ")";
}

bool InputEventScreenDrag::accumulate(const Ref<InputEvent> &p_event) {
Ref<InputEventScreenDrag> drag = p_event;
if (drag.is_null())
return false;
pouleyKetchoupp marked this conversation as resolved.
Show resolved Hide resolved

if (get_index() != drag->get_index()) {
return false;
}

set_position(drag->get_position());
set_speed(drag->get_speed());
relative += drag->get_relative();

return true;
}

void InputEventScreenDrag::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenDrag::set_index);
ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenDrag::get_index);
Expand Down
2 changes: 2 additions & 0 deletions core/os/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ class InputEventScreenDrag : public InputEvent {
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
virtual String as_text() const;

virtual bool accumulate(const Ref<InputEvent> &p_event);

InputEventScreenDrag();
};

Expand Down
6 changes: 6 additions & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,12 @@
Default [InputEventAction] to move up in the UI.
[b]Note:[/b] Default [code]ui_*[/code] actions cannot be removed as they are necessary for the internal logic of several [Control]s. The events assigned to the action can however be modified.
</member>
<member name="input_devices/buffering/agile_event_flushing" type="bool" setter="" getter="" default="false">
If [code]true[/code], key/touch/joystick events will be flushed just before every idle and physics frame.
If [code]false[/code], such events will be flushed only once per idle frame, between iterations of the engine.
Enabling this can greatly improve the responsiveness to input, specially in devices that need to run multiple physics frames per visible (idle) frame, because they can't run at the target frame rate.
[b]Note:[/b] Currently implemented only in Android.
</member>
<member name="input_devices/pointing/emulate_mouse_from_touch" type="bool" setter="" getter="" default="true">
If [code]true[/code], sends mouse input events when tapping or swiping on the touchscreen.
</member>
Expand Down
47 changes: 27 additions & 20 deletions main/input_default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,6 @@ Vector3 InputDefault::get_gyroscope() const {
return gyroscope;
}

void InputDefault::parse_input_event(const Ref<InputEvent> &p_event) {
_parse_input_event_impl(p_event, false);
}

void InputDefault::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated) {
// Notes on mouse-touch emulation:
// - Emulated mouse events are parsed, that is, re-routed to this method, so they make the same effects
Expand All @@ -327,8 +323,6 @@ void InputDefault::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool
// - Emulated touch events are handed right to the main loop (i.e., the SceneTree) because they don't
// require additional handling by this class.

_THREAD_SAFE_METHOD_

Ref<InputEventKey> k = p_event;
if (k.is_valid() && !k->is_echo() && k->get_scancode() != 0) {
if (k->is_pressed()) {
Expand Down Expand Up @@ -697,32 +691,44 @@ void InputDefault::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_sh
OS::get_singleton()->set_custom_mouse_cursor(p_cursor, (OS::CursorShape)p_shape, p_hotspot);
}

void InputDefault::accumulate_input_event(const Ref<InputEvent> &p_event) {
void InputDefault::parse_input_event(const Ref<InputEvent> &p_event) {
_THREAD_SAFE_METHOD_

ERR_FAIL_COND(p_event.is_null());

if (!use_accumulated_input) {
parse_input_event(p_event);
return;
if (use_accumulated_input) {
if (buffered_events.empty() || !buffered_events.back()->get()->accumulate(p_event)) {
buffered_events.push_back(p_event);
}
} else if (use_input_buffering) {
buffered_events.push_back(p_event);
} else {
_parse_input_event_impl(p_event, false);
}
if (!accumulated_events.empty() && accumulated_events.back()->get()->accumulate(p_event)) {
return; //event was accumulated, exit
}
void InputDefault::flush_buffered_events() {
_THREAD_SAFE_METHOD_

while (buffered_events.front()) {
_parse_input_event_impl(buffered_events.front()->get(), false);
buffered_events.pop_front();
}
}

accumulated_events.push_back(p_event);
bool InputDefault::is_using_input_buffering() {
return use_input_buffering;
}
void InputDefault::flush_accumulated_events() {
while (accumulated_events.front()) {
parse_input_event(accumulated_events.front()->get());
accumulated_events.pop_front();
}

void InputDefault::set_use_input_buffering(bool p_enable) {
use_input_buffering = p_enable;
}

void InputDefault::set_use_accumulated_input(bool p_enable) {
use_accumulated_input = p_enable;
}

void InputDefault::release_pressed_events() {
flush_accumulated_events(); // this is needed to release actions strengths
flush_buffered_events(); // this is needed to release actions strengths

keys_pressed.clear();
joy_buttons_pressed.clear();
Expand All @@ -736,7 +742,8 @@ void InputDefault::release_pressed_events() {
}

InputDefault::InputDefault() {
use_accumulated_input = true;
use_input_buffering = false;
use_accumulated_input = false;
mouse_button_mask = 0;
emulate_touch_from_mouse = false;
emulate_mouse_from_touch = false;
Expand Down
8 changes: 5 additions & 3 deletions main/input_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ class InputDefault : public Input {

void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated);

List<Ref<InputEvent>> accumulated_events;
List<Ref<InputEvent>> buffered_events;
bool use_input_buffering;
bool use_accumulated_input;

protected:
Expand Down Expand Up @@ -302,8 +303,9 @@ class InputDefault : public Input {
String get_joy_guid_remapped(int p_device) const;
void set_fallback_mapping(String p_guid);

virtual void accumulate_input_event(const Ref<InputEvent> &p_event);
virtual void flush_accumulated_events();
virtual void flush_buffered_events();
virtual bool is_using_input_buffering();
virtual void set_use_input_buffering(bool p_enable);
virtual void set_use_accumulated_input(bool p_enable);

virtual void release_pressed_events();
Expand Down
23 changes: 20 additions & 3 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,8 @@ Error Main::setup2(Thread::ID p_main_tid_override) {

InputDefault *id = Object::cast_to<InputDefault>(Input::get_singleton());
if (id) {
agile_input_event_flushing = GLOBAL_DEF("input_devices/buffering/agile_event_flushing", false);

m4gr3d marked this conversation as resolved.
Show resolved Hide resolved
if (bool(GLOBAL_DEF("input_devices/pointing/emulate_touch_from_mouse", false)) && !(editor || project_manager)) {
if (!OS::get_singleton()->has_touchscreen_ui_hint()) {
//only if no touchscreen ui hint, set emulation
Expand Down Expand Up @@ -2045,6 +2047,8 @@ uint32_t Main::frames = 0;
uint32_t Main::frame = 0;
bool Main::force_redraw_requested = false;
int Main::iterating = 0;
bool Main::agile_input_event_flushing = false;

bool Main::is_iterating() {
return iterating > 0;
}
Expand Down Expand Up @@ -2114,9 +2118,13 @@ bool Main::iteration() {

bool exit = false;

Engine::get_singleton()->_in_physics = true;

for (int iters = 0; iters < advance.physics_steps; ++iters) {
if (InputDefault::get_singleton()->is_using_input_buffering() && agile_input_event_flushing) {
InputDefault::get_singleton()->flush_buffered_events();
}

Engine::get_singleton()->_in_physics = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any risks tied to toggling this on/off for each physics iteration like this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to decide one way or the other (since I don't see a strong reason for either), I decided this, so the input callbacks are still called with Engine::is_in_physics_frame() being false, to make it a bit more transparent to the game.

I can't see any potential risks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's hard to predict potential side effects, but it makes sense to keep input events reported as not being part of the physics tick, and we'll see if it causes issues in some edge cases by testing over time.


uint64_t physics_begin = OS::get_singleton()->get_ticks_usec();

PhysicsServer::get_singleton()->flush_queries();
Expand All @@ -2141,9 +2149,13 @@ bool Main::iteration() {
physics_process_ticks = MAX(physics_process_ticks, OS::get_singleton()->get_ticks_usec() - physics_begin); // keep the largest one for reference
physics_process_max = MAX(OS::get_singleton()->get_ticks_usec() - physics_begin, physics_process_max);
Engine::get_singleton()->_physics_frames++;

Engine::get_singleton()->_in_physics = false;
}

Engine::get_singleton()->_in_physics = false;
if (InputDefault::get_singleton()->is_using_input_buffering() && agile_input_event_flushing) {
InputDefault::get_singleton()->flush_buffered_events();
}

uint64_t idle_begin = OS::get_singleton()->get_ticks_usec();

Expand Down Expand Up @@ -2217,6 +2229,11 @@ bool Main::iteration() {

iterating--;

// Needed for OSs using input buffering regardless accumulation (like Android)
if (InputDefault::get_singleton()->is_using_input_buffering() && !agile_input_event_flushing) {
InputDefault::get_singleton()->flush_buffered_events();
}

if (fixed_fps != -1) {
return exit;
}
Expand Down
1 change: 1 addition & 0 deletions main/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Main {
static uint32_t frame;
static bool force_redraw_requested;
static int iterating;
static bool agile_input_event_flushing;

public:
static bool is_project_manager();
Expand Down
1 change: 1 addition & 0 deletions platform/android/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Import("env")

android_files = [
"os_android.cpp",
"android_input_handler.cpp",
"file_access_android.cpp",
"audio_driver_opensl.cpp",
"dir_access_jandroid.cpp",
Expand Down
Loading