diff options
author | Chris Loer <chris.loer@gmail.com> | 2017-05-17 13:49:48 -0700 |
---|---|---|
committer | Chris Loer <chris.loer@mapbox.com> | 2017-05-17 16:25:06 -0700 |
commit | 77b24bc0d3ddfda494cf1a7ce4090ff3a79324e4 (patch) | |
tree | cc5866773c159f1b3573f507069bf1c26660d560 /src/mbgl/util | |
parent | e7154adac3985b8d877c40d16c7d4b3916c7246b (diff) | |
download | qtlocation-mapboxgl-77b24bc0d3ddfda494cf1a7ce4090ff3a79324e4.tar.gz |
[core] Throttle tiles to redo symbol placement at most once every 300ms.
Fixes issue #8435 and prepares for pitch-scaling changes in issue #8967.
Diffstat (limited to 'src/mbgl/util')
-rw-r--r-- | src/mbgl/util/throttler.cpp | 36 | ||||
-rw-r--r-- | src/mbgl/util/throttler.hpp | 22 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/mbgl/util/throttler.cpp b/src/mbgl/util/throttler.cpp new file mode 100644 index 0000000000..910810ce2f --- /dev/null +++ b/src/mbgl/util/throttler.cpp @@ -0,0 +1,36 @@ +#include <mbgl/util/throttler.hpp> + +namespace mbgl { +namespace util { + +Throttler::Throttler(Duration frequency_, std::function<void()>&& function_) + : frequency(frequency_) + , function(std::move(function_)) + , pendingInvocation(false) + , lastInvocation(TimePoint::min()) +{} + +void Throttler::invoke() { + if (pendingInvocation) { + return; + } + + Duration timeToNextInvocation = lastInvocation == TimePoint::min() + ? Duration::zero() + : (lastInvocation + frequency) - Clock::now(); + + if (timeToNextInvocation <= Duration::zero()) { + lastInvocation = Clock::now(); + function(); + } else { + pendingInvocation = true; + timer.start(timeToNextInvocation, Duration::zero(), [this]{ + pendingInvocation = false; + lastInvocation = Clock::now(); + function(); + }); + } +} + +} // namespace util +} // namespace mbgl diff --git a/src/mbgl/util/throttler.hpp b/src/mbgl/util/throttler.hpp new file mode 100644 index 0000000000..175de7ccaf --- /dev/null +++ b/src/mbgl/util/throttler.hpp @@ -0,0 +1,22 @@ +#include <mbgl/util/chrono.hpp> +#include <mbgl/util/timer.hpp> + +namespace mbgl { +namespace util { + +class Throttler { +public: + Throttler(Duration frequency, std::function<void()>&& function); + + void invoke(); +private: + Duration frequency; + std::function<void()> function; + + Timer timer; + bool pendingInvocation; + TimePoint lastInvocation; +}; + +} // namespace util +} // namespace mbgl |