diff --git a/android/src/com/mapswithme/maps/MapFragment.java b/android/src/com/mapswithme/maps/MapFragment.java index c90819cb7f..951a4a117a 100644 --- a/android/src/com/mapswithme/maps/MapFragment.java +++ b/android/src/com/mapswithme/maps/MapFragment.java @@ -195,10 +195,7 @@ public class MapFragment extends BaseMwmFragment if (!mEngineCreated) return; - // We're in the main thread here. So nothing from the queue will be run between these two calls. - // Destroy engine first, then clear the queue that theoretically can be filled by nativeDestroyEngine(). nativeDestroyEngine(); - MwmApplication.get().clearFunctorsOnUiThread(); mEngineCreated = false; } diff --git a/android/src/com/mapswithme/maps/MwmApplication.java b/android/src/com/mapswithme/maps/MwmApplication.java index ff1780e27f..71553beea2 100644 --- a/android/src/com/mapswithme/maps/MwmApplication.java +++ b/android/src/com/mapswithme/maps/MwmApplication.java @@ -292,11 +292,6 @@ public class MwmApplication extends Application mMainLoopHandler.sendMessage(m); } - void clearFunctorsOnUiThread() - { - mMainLoopHandler.removeCallbacksAndMessages(mMainQueueToken); - } - /** * Initializes native Platform with paths. Should be called before usage of any other native components. */ diff --git a/drape_frontend/drape_engine.cpp b/drape_frontend/drape_engine.cpp index ee62f04e5e..3a5e767d0f 100644 --- a/drape_frontend/drape_engine.cpp +++ b/drape_frontend/drape_engine.cpp @@ -8,7 +8,6 @@ #include "drape/texture_manager.hpp" -#include "platform/platform.hpp" #include "platform/settings.hpp" #include "std/bind.hpp" @@ -130,18 +129,6 @@ void DrapeEngine::SetModelViewAnyRect(m2::AnyRectD const & rect, bool isAnim) AddUserEvent(SetAnyRectEvent(rect, isAnim)); } -int DrapeEngine::AddModelViewListener(TModelViewListenerFn const & listener) -{ - static int currentSlotID = 0; - VERIFY(m_listeners.insert(make_pair(++currentSlotID, listener)).second, ()); - return currentSlotID; -} - -void DrapeEngine::RemoveModeViewListener(int slotID) -{ - m_listeners.erase(slotID); -} - void DrapeEngine::ClearUserMarksLayer(df::TileKey const & tileKey) { m_threadCommutator->PostMessage(ThreadsCommutator::RenderThread, @@ -213,42 +200,27 @@ void DrapeEngine::AddUserEvent(UserEvent const & e) void DrapeEngine::ModelViewChanged(ScreenBase const & screen) { - Platform & pl = GetPlatform(); - pl.RunOnGuiThread(bind(&DrapeEngine::ModelViewChangedGuiThread, this, screen)); -} - -void DrapeEngine::ModelViewChangedGuiThread(ScreenBase const & screen) -{ - for (pair const & p : m_listeners) - p.second(screen); + if (m_modelViewChanged != nullptr) + m_modelViewChanged(screen); } void DrapeEngine::MyPositionModeChanged(location::EMyPositionMode mode, bool routingActive) { settings::Set(settings::kLocationStateMode, mode); - GetPlatform().RunOnGuiThread([this, mode, routingActive]() - { - if (m_myPositionModeChanged != nullptr) - m_myPositionModeChanged(mode, routingActive); - }); + if (m_myPositionModeChanged != nullptr) + m_myPositionModeChanged(mode, routingActive); } void DrapeEngine::TapEvent(TapInfo const & tapInfo) { - GetPlatform().RunOnGuiThread([=]() - { - if (m_tapListener) - m_tapListener(tapInfo); - }); + if (m_tapListener != nullptr) + m_tapListener(tapInfo); } void DrapeEngine::UserPositionChanged(m2::PointD const & position) { - GetPlatform().RunOnGuiThread([this, position]() - { - if (m_userPositionChangedFn) - m_userPositionChangedFn(position); - }); + if (m_userPositionChanged != nullptr) + m_userPositionChanged(position); } void DrapeEngine::ResizeImpl(int w, int h) @@ -301,19 +273,24 @@ void DrapeEngine::FollowRoute(int preferredZoomLevel, int preferredZoomLevel3d, MessagePriority::High); } -void DrapeEngine::SetMyPositionModeListener(location::TMyPositionModeChanged const & fn) +void DrapeEngine::SetModelViewListener(TModelViewListenerFn && fn) { - m_myPositionModeChanged = fn; + m_modelViewChanged = move(fn); } -void DrapeEngine::SetTapEventInfoListener(TTapEventInfoFn const & fn) +void DrapeEngine::SetMyPositionModeListener(location::TMyPositionModeChanged && fn) { - m_tapListener = fn; + m_myPositionModeChanged = move(fn); } -void DrapeEngine::SetUserPositionListener(DrapeEngine::TUserPositionChangedFn const & fn) +void DrapeEngine::SetTapEventInfoListener(TTapEventInfoFn && fn) { - m_userPositionChangedFn = fn; + m_tapListener = move(fn); +} + +void DrapeEngine::SetUserPositionListener(TUserPositionChangedFn && fn) +{ + m_userPositionChanged = move(fn); } FeatureID DrapeEngine::GetVisiblePOI(m2::PointD const & glbPoint) diff --git a/drape_frontend/drape_engine.hpp b/drape_frontend/drape_engine.hpp index 58272583cf..dad1982e66 100644 --- a/drape_frontend/drape_engine.hpp +++ b/drape_frontend/drape_engine.hpp @@ -92,8 +92,7 @@ public: void SetModelViewAnyRect(m2::AnyRectD const & rect, bool isAnim); using TModelViewListenerFn = FrontendRenderer::TModelViewChanged; - int AddModelViewListener(TModelViewListenerFn const & listener); - void RemoveModeViewListener(int slotID); + void SetModelViewListener(TModelViewListenerFn && fn); void ClearUserMarksLayer(TileKey const & tileKey); void ChangeVisibilityUserMarksLayer(TileKey const & tileKey, bool isVisible); @@ -108,12 +107,12 @@ public: void SwitchMyPositionNextMode(); void LoseLocation(); void StopLocationFollow(); - void SetMyPositionModeListener(location::TMyPositionModeChanged const & fn); + void SetMyPositionModeListener(location::TMyPositionModeChanged && fn); using TTapEventInfoFn = FrontendRenderer::TTapEventInfoFn; - void SetTapEventInfoListener(TTapEventInfoFn const & fn); + void SetTapEventInfoListener(TTapEventInfoFn && fn); using TUserPositionChangedFn = FrontendRenderer::TUserPositionChangedFn; - void SetUserPositionListener(TUserPositionChangedFn const & fn); + void SetUserPositionListener(TUserPositionChangedFn && fn); FeatureID GetVisiblePOI(m2::PointD const & glbPoint); void SelectObject(SelectionShape::ESelectedObject obj, m2::PointD const & pt, bool isAnim); @@ -149,7 +148,6 @@ public: private: void AddUserEvent(UserEvent const & e); void ModelViewChanged(ScreenBase const & screen); - void ModelViewChangedGuiThread(ScreenBase const & screen); void MyPositionModeChanged(location::EMyPositionMode mode, bool routingActive); void TapEvent(TapInfo const & tapInfo); @@ -167,12 +165,10 @@ private: Viewport m_viewport; - using TListenerMap = map; - TListenerMap m_listeners; - + TModelViewListenerFn m_modelViewChanged; location::TMyPositionModeChanged m_myPositionModeChanged; + TUserPositionChangedFn m_userPositionChanged; TTapEventInfoFn m_tapListener; - TUserPositionChangedFn m_userPositionChangedFn; gui::TWidgetsInitInfo m_widgetsInfo; gui::TWidgetsLayoutInfo m_widgetsLayout; diff --git a/drape_frontend/frontend_renderer.cpp b/drape_frontend/frontend_renderer.cpp index 39bb090825..93af6a854a 100755 --- a/drape_frontend/frontend_renderer.cpp +++ b/drape_frontend/frontend_renderer.cpp @@ -1543,10 +1543,7 @@ void FrontendRenderer::Routine::Do() m_renderer.RenderScene(modelView); if (modelViewChanged) - { m_renderer.UpdateScene(modelView); - m_renderer.EmitModelViewChanged(modelView); - } isActiveFrame |= InterpolationHolder::Instance().Advance(frameTime); AnimationSystem::Instance().Advance(frameTime); @@ -1687,6 +1684,7 @@ void FrontendRenderer::UpdateScene(ScreenBase const & modelView) if (m_lastReadedModelView != modelView) { + EmitModelViewChanged(modelView); m_lastReadedModelView = modelView; m_requestedTiles->Set(modelView, m_isIsometry || modelView.isPerspective(), ResolveTileKeys(modelView)); m_commutator->PostMessage(ThreadsCommutator::ResourceUploadThread, diff --git a/map/framework.cpp b/map/framework.cpp index 288a20955e..6fd5d15eb4 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -225,6 +225,17 @@ void Framework::OnUserPositionChanged(m2::PointD const & position) m_routingSession.SetUserCurrentPosition(position); } +void Framework::OnViewportChanged(ScreenBase const & screen) +{ + if (!screen.GlobalRect().EqualDxDy(m_currentModelView.GlobalRect(), 1.0E-4)) + UpdateUserViewportChanged(); + + m_currentModelView = screen; + + if (m_viewportChanged != nullptr) + m_viewportChanged(screen); +} + void Framework::CallDrapeFunction(TDrapeFunction const & fn) const { if (m_drapeEngine) @@ -942,16 +953,9 @@ void Framework::GetTouchRect(m2::PointD const & center, uint32_t pxRadius, m2::A m_currentModelView.GetTouchRect(center, static_cast(pxRadius), rect); } -int Framework::AddViewportListener(TViewportChanged const & fn) +void Framework::SetViewportListener(TViewportChanged const & fn) { - ASSERT(m_drapeEngine, ()); - return m_drapeEngine->AddModelViewListener(fn); -} - -void Framework::RemoveViewportListener(int slotID) -{ - ASSERT(m_drapeEngine, ()); - m_drapeEngine->RemoveModeViewListener(slotID); + m_viewportChanged = fn; } void Framework::OnSize(int w, int h) @@ -1510,17 +1514,29 @@ void Framework::CreateDrapeEngine(ref_ptr contextFactory, m_routingSession.IsActive() && m_routingSession.IsFollowing()); m_drapeEngine = make_unique_dp(move(p)); - AddViewportListener([this](ScreenBase const & screen) + m_drapeEngine->SetModelViewListener([this](ScreenBase const & screen) { - if (!screen.GlobalRect().EqualDxDy(m_currentModelView.GlobalRect(), 1.0E-4)) - UpdateUserViewportChanged(); - m_currentModelView = screen; + GetPlatform().RunOnGuiThread([this, screen](){ OnViewportChanged(screen); }); }); - m_drapeEngine->SetTapEventInfoListener(bind(&Framework::OnTapEvent, this, _1)); - m_drapeEngine->SetUserPositionListener(bind(&Framework::OnUserPositionChanged, this, _1)); + m_drapeEngine->SetTapEventInfoListener([this](df::TapInfo const & tapInfo) + { + GetPlatform().RunOnGuiThread([this, tapInfo](){ OnTapEvent(tapInfo); }); + }); + m_drapeEngine->SetUserPositionListener([this](m2::PointD const & position) + { + GetPlatform().RunOnGuiThread([this, position](){ OnUserPositionChanged(position); }); + }); + OnSize(params.m_surfaceWidth, params.m_surfaceHeight); - m_drapeEngine->SetMyPositionModeListener(m_myPositionListener); + m_drapeEngine->SetMyPositionModeListener([this](location::EMyPositionMode mode, bool routingActive) + { + GetPlatform().RunOnGuiThread([this, mode, routingActive]() + { + if (m_myPositionListener != nullptr) + m_myPositionListener(mode, routingActive); + }); + }); InvalidateUserMarks(); diff --git a/map/framework.hpp b/map/framework.hpp index 5e44ad9786..473e46f59b 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -120,6 +120,9 @@ protected: ScreenBase m_currentModelView; + using TViewportChanged = df::DrapeEngine::TModelViewListenerFn; + TViewportChanged m_viewportChanged; + routing::RoutingSession m_routingSession; drape_ptr m_drapeEngine; @@ -152,6 +155,8 @@ protected: void StopLocationFollow(); + void OnViewportChanged(ScreenBase const & screen); + void CallDrapeFunction(TDrapeFunction const & fn) const; public: @@ -463,9 +468,7 @@ public: void GetTouchRect(m2::PointD const & center, uint32_t pxRadius, m2::AnyRectD & rect); - using TViewportChanged = df::DrapeEngine::TModelViewListenerFn; - int AddViewportListener(TViewportChanged const & fn); - void RemoveViewportListener(int slotID); + void SetViewportListener(TViewportChanged const & fn); /// Resize event from window. void OnSize(int w, int h); diff --git a/qt/draw_widget.cpp b/qt/draw_widget.cpp index 87edba43e7..ba54ad127b 100644 --- a/qt/draw_widget.cpp +++ b/qt/draw_widget.cpp @@ -257,7 +257,7 @@ void DrawWidget::CreateEngine() p.m_widgetsInitInfo[gui::WIDGET_SCALE_LABEL] = gui::Position(dp::LeftBottom); m_framework->CreateDrapeEngine(make_ref(m_contextFactory), std::move(p)); - m_framework->AddViewportListener(bind(&DrawWidget::OnViewportChanged, this, _1)); + m_framework->SetViewportListener(bind(&DrawWidget::OnViewportChanged, this, _1)); } void DrawWidget::initializeGL()