diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/input/input.cpp | 26 | ||||
-rw-r--r-- | core/io/dir_access.h | 2 | ||||
-rw-r--r-- | core/io/file_access.h | 2 | ||||
-rw-r--r-- | core/math/basis.cpp | 4 | ||||
-rw-r--r-- | core/math/convex_hull.cpp | 20 | ||||
-rw-r--r-- | core/math/math_funcs.h | 10 | ||||
-rw-r--r-- | core/math/quaternion.cpp | 3 | ||||
-rw-r--r-- | core/math/static_raycaster.h | 16 | ||||
-rw-r--r-- | core/os/time.cpp | 8 | ||||
-rw-r--r-- | core/templates/local_vector.h | 15 |
10 files changed, 62 insertions, 44 deletions
diff --git a/core/input/input.cpp b/core/input/input.cpp index e74523e059..2b3e0b56e4 100644 --- a/core/input/input.cpp +++ b/core/input/input.cpp @@ -35,6 +35,10 @@ #include "core/input/input_map.h" #include "core/os/os.h" +#ifdef DEV_ENABLED +#include "core/os/thread.h" +#endif + static const char *_joy_buttons[(size_t)JoyButton::SDL_MAX] = { "a", "b", @@ -486,6 +490,10 @@ Vector3 Input::get_gyroscope() const { } void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated) { + // This function does the final delivery of the input event to user land. + // Regardless where the event came from originally, this has to happen on the main thread. + DEV_ASSERT(Thread::get_caller_id() == Thread::get_main_id()); + // Notes on mouse-touch emulation: // - Emulated mouse events are parsed, that is, re-routed to this method, so they make the same effects // as true mouse events. The only difference is the situation is flagged as emulated so they are not @@ -537,7 +545,9 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em touch_event->set_position(mb->get_position()); touch_event->set_double_tap(mb->is_double_click()); touch_event->set_device(InputEvent::DEVICE_ID_EMULATION); + _THREAD_SAFE_UNLOCK_ event_dispatch_function(touch_event); + _THREAD_SAFE_LOCK_ } } @@ -563,7 +573,9 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em drag_event->set_velocity(get_last_mouse_velocity()); drag_event->set_device(InputEvent::DEVICE_ID_EMULATION); + _THREAD_SAFE_UNLOCK_ event_dispatch_function(drag_event); + _THREAD_SAFE_LOCK_ } } @@ -664,7 +676,9 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em if (ge.is_valid()) { if (event_dispatch_function) { + _THREAD_SAFE_UNLOCK_ event_dispatch_function(ge); + _THREAD_SAFE_LOCK_ } } @@ -687,7 +701,9 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em } if (event_dispatch_function) { + _THREAD_SAFE_UNLOCK_ event_dispatch_function(p_event); + _THREAD_SAFE_LOCK_ } } @@ -831,6 +847,7 @@ bool Input::is_emulating_touch_from_mouse() const { // Calling this whenever the game window is focused helps unsticking the "touch mouse" // if the OS or its abstraction class hasn't properly reported that touch pointers raised void Input::ensure_touch_mouse_raised() { + _THREAD_SAFE_METHOD_ if (mouse_from_touch_index != -1) { mouse_from_touch_index = -1; @@ -937,8 +954,15 @@ void Input::flush_buffered_events() { _THREAD_SAFE_METHOD_ while (buffered_events.front()) { - _parse_input_event_impl(buffered_events.front()->get(), false); + // The final delivery of the input event involves releasing the lock. + // While the lock is released, another thread may lock it and add new events to the back. + // Therefore, we get each event and pop it while we still have the lock, + // to ensure the list is in a consistent state. + List<Ref<InputEvent>>::Element *E = buffered_events.front(); + Ref<InputEvent> e = E->get(); buffered_events.pop_front(); + + _parse_input_event_impl(e, false); } } diff --git a/core/io/dir_access.h b/core/io/dir_access.h index 51eb68eaea..52ed688deb 100644 --- a/core/io/dir_access.h +++ b/core/io/dir_access.h @@ -68,7 +68,7 @@ protected: virtual String _get_root_string() const; AccessType get_access_type() const; - String fix_path(String p_path) const; + virtual String fix_path(String p_path) const; template <class T> static Ref<DirAccess> _create_builtin() { diff --git a/core/io/file_access.h b/core/io/file_access.h index 47770cad87..3374dca7a1 100644 --- a/core/io/file_access.h +++ b/core/io/file_access.h @@ -80,7 +80,7 @@ protected: static void _bind_methods(); AccessType get_access_type() const; - String fix_path(const String &p_path) const; + virtual String fix_path(const String &p_path) const; virtual Error open_internal(const String &p_path, int p_mode_flags) = 0; ///< open a file virtual uint64_t _get_modified_time(const String &p_file) = 0; virtual void _set_access_type(AccessType p_access); diff --git a/core/math/basis.cpp b/core/math/basis.cpp index 95a4187062..bfd902c7e2 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -807,8 +807,8 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { z = (rows[1][0] - rows[0][1]) / s; r_axis = Vector3(x, y, z); - // CLAMP to avoid NaN if the value passed to acos is not in [0,1]. - r_angle = Math::acos(CLAMP((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2, (real_t)0.0, (real_t)1.0)); + // acos does clamping. + r_angle = Math::acos((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2); } void Basis::set_quaternion(const Quaternion &p_quaternion) { diff --git a/core/math/convex_hull.cpp b/core/math/convex_hull.cpp index a03438a339..76b3062944 100644 --- a/core/math/convex_hull.cpp +++ b/core/math/convex_hull.cpp @@ -596,9 +596,9 @@ private: } }; - enum Orientation { NONE, - CLOCKWISE, - COUNTER_CLOCKWISE }; + enum Orientation { ORIENTATION_NONE, + ORIENTATION_CLOCKWISE, + ORIENTATION_COUNTER_CLOCKWISE }; Vector3 scaling; Vector3 center; @@ -1140,13 +1140,13 @@ ConvexHullInternal::Orientation ConvexHullInternal::get_orientation(const Edge * CHULL_ASSERT(!m.is_zero()); int64_t dot = n.dot(m); CHULL_ASSERT(dot != 0); - return (dot > 0) ? COUNTER_CLOCKWISE : CLOCKWISE; + return (dot > 0) ? ORIENTATION_COUNTER_CLOCKWISE : ORIENTATION_CLOCKWISE; } - return COUNTER_CLOCKWISE; + return ORIENTATION_COUNTER_CLOCKWISE; } else if (p_prev->prev == p_next) { - return CLOCKWISE; + return ORIENTATION_CLOCKWISE; } else { - return NONE; + return ORIENTATION_NONE; } } @@ -1176,7 +1176,7 @@ ConvexHullInternal::Edge *ConvexHullInternal::find_max_angle(bool p_ccw, const V } else if ((cmp = cot.compare(p_min_cot)) < 0) { p_min_cot = cot; min_edge = e; - } else if ((cmp == 0) && (p_ccw == (get_orientation(min_edge, e, p_s, t) == COUNTER_CLOCKWISE))) { + } else if ((cmp == 0) && (p_ccw == (get_orientation(min_edge, e, p_s, t) == ORIENTATION_COUNTER_CLOCKWISE))) { min_edge = e; } } @@ -1375,7 +1375,7 @@ void ConvexHullInternal::merge(IntermediateHull &p_h0, IntermediateHull &p_h1) { int64_t dot = (*e->target - *c0).dot(normal); CHULL_ASSERT(dot <= 0); if ((dot == 0) && ((*e->target - *c0).dot(t) > 0)) { - if (!start0 || (get_orientation(start0, e, s, Point32(0, 0, -1)) == CLOCKWISE)) { + if (!start0 || (get_orientation(start0, e, s, Point32(0, 0, -1)) == ORIENTATION_CLOCKWISE)) { start0 = e; } } @@ -1390,7 +1390,7 @@ void ConvexHullInternal::merge(IntermediateHull &p_h0, IntermediateHull &p_h1) { int64_t dot = (*e->target - *c1).dot(normal); CHULL_ASSERT(dot <= 0); if ((dot == 0) && ((*e->target - *c1).dot(t) > 0)) { - if (!start1 || (get_orientation(start1, e, s, Point32(0, 0, -1)) == COUNTER_CLOCKWISE)) { + if (!start1 || (get_orientation(start1, e, s, Point32(0, 0, -1)) == ORIENTATION_COUNTER_CLOCKWISE)) { start1 = e; } } diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 078320d620..f96d3a909f 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -74,11 +74,13 @@ public: static _ALWAYS_INLINE_ double tanh(double p_x) { return ::tanh(p_x); } static _ALWAYS_INLINE_ float tanh(float p_x) { return ::tanhf(p_x); } - static _ALWAYS_INLINE_ double asin(double p_x) { return ::asin(p_x); } - static _ALWAYS_INLINE_ float asin(float p_x) { return ::asinf(p_x); } + // Always does clamping so always safe to use. + static _ALWAYS_INLINE_ double asin(double p_x) { return p_x < -1 ? (-Math_PI / 2) : (p_x > 1 ? (Math_PI / 2) : ::asin(p_x)); } + static _ALWAYS_INLINE_ float asin(float p_x) { return p_x < -1 ? (-Math_PI / 2) : (p_x > 1 ? (Math_PI / 2) : ::asinf(p_x)); } - static _ALWAYS_INLINE_ double acos(double p_x) { return ::acos(p_x); } - static _ALWAYS_INLINE_ float acos(float p_x) { return ::acosf(p_x); } + // Always does clamping so always safe to use. + static _ALWAYS_INLINE_ double acos(double p_x) { return p_x < -1 ? Math_PI : (p_x > 1 ? 0 : ::acos(p_x)); } + static _ALWAYS_INLINE_ float acos(float p_x) { return p_x < -1 ? Math_PI : (p_x > 1 ? 0 : ::acosf(p_x)); } static _ALWAYS_INLINE_ double atan(double p_x) { return ::atan(p_x); } static _ALWAYS_INLINE_ float atan(float p_x) { return ::atanf(p_x); } diff --git a/core/math/quaternion.cpp b/core/math/quaternion.cpp index 34e212a5b6..e4ad17c8ef 100644 --- a/core/math/quaternion.cpp +++ b/core/math/quaternion.cpp @@ -35,7 +35,8 @@ real_t Quaternion::angle_to(const Quaternion &p_to) const { real_t d = dot(p_to); - return Math::acos(CLAMP(d * d * 2 - 1, -1, 1)); + // acos does clamping. + return Math::acos(d * d * 2 - 1); } Vector3 Quaternion::get_euler(EulerOrder p_order) const { diff --git a/core/math/static_raycaster.h b/core/math/static_raycaster.h index 1bafc29c57..c53868e12d 100644 --- a/core/math/static_raycaster.h +++ b/core/math/static_raycaster.h @@ -59,15 +59,15 @@ public: /*! Constructs a ray from origin, direction, and ray segment. Near * has to be smaller than far. */ - _FORCE_INLINE_ Ray(const Vector3 &org, - const Vector3 &dir, - float tnear = 0.0f, - float tfar = INFINITY) : - org(org), - tnear(tnear), - dir(dir), + _FORCE_INLINE_ Ray(const Vector3 &p_org, + const Vector3 &p_dir, + float p_tnear = 0.0f, + float p_tfar = INFINITY) : + org(p_org), + tnear(p_tnear), + dir(p_dir), time(0.0f), - tfar(tfar), + tfar(p_tfar), mask(-1), u(0.0), v(0.0), diff --git a/core/os/time.cpp b/core/os/time.cpp index 12e6f08525..038e4adc03 100644 --- a/core/os/time.cpp +++ b/core/os/time.cpp @@ -382,10 +382,10 @@ String Time::get_time_string_from_system(bool p_utc) const { Dictionary Time::get_time_zone_from_system() const { OS::TimeZoneInfo info = OS::get_singleton()->get_time_zone_info(); - Dictionary timezone; - timezone["bias"] = info.bias; - timezone["name"] = info.name; - return timezone; + Dictionary ret_timezone; + ret_timezone["bias"] = info.bias; + ret_timezone["name"] = info.name; + return ret_timezone; } double Time::get_unix_time_from_system() const { diff --git a/core/templates/local_vector.h b/core/templates/local_vector.h index 5311a94987..22db3a5744 100644 --- a/core/templates/local_vector.h +++ b/core/templates/local_vector.h @@ -59,11 +59,7 @@ public: _FORCE_INLINE_ void push_back(T p_elem) { if (unlikely(count == capacity)) { - if (capacity == 0) { - capacity = 1; - } else { - capacity <<= 1; - } + capacity = tight ? (capacity + 1) : MAX((U)1, capacity << 1); data = (T *)memrealloc(data, capacity * sizeof(T)); CRASH_COND_MSG(!data, "Out of memory"); } @@ -87,7 +83,7 @@ public: } /// Removes the item copying the last value into the position of the one to - /// remove. It's generally faster than `remove`. + /// remove. It's generally faster than `remove_at`. void remove_at_unordered(U p_index) { ERR_FAIL_INDEX(p_index, count); count--; @@ -143,12 +139,7 @@ public: count = p_size; } else if (p_size > count) { if (unlikely(p_size > capacity)) { - if (capacity == 0) { - capacity = 1; - } - while (capacity < p_size) { - capacity <<= 1; - } + capacity = tight ? p_size : nearest_power_of_2_templated(p_size); data = (T *)memrealloc(data, capacity * sizeof(T)); CRASH_COND_MSG(!data, "Out of memory"); } |