From 4525530310e8e02255508c7c3bc7d61cb125e427 Mon Sep 17 00:00:00 2001 From: Paul Rouget Date: Tue, 30 Jul 2019 21:17:43 +0200 Subject: [PATCH] Use tasks instead of events --- support/hololens/ServoApp/BrowserPage.cpp | 61 ++++++----------------- support/hololens/ServoApp/BrowserPage.h | 12 ++--- support/hololens/ServoApp/Servo.cpp | 1 - 3 files changed, 19 insertions(+), 55 deletions(-) diff --git a/support/hololens/ServoApp/BrowserPage.cpp b/support/hololens/ServoApp/BrowserPage.cpp index 1b4844d40bcb..d6681375d11a 100644 --- a/support/hololens/ServoApp/BrowserPage.cpp +++ b/support/hololens/ServoApp/BrowserPage.cpp @@ -36,7 +36,7 @@ void BrowserPage::Shutdown() { // shutdown event to Servo. } else { HANDLE hEvent = ::CreateEventA(nullptr, FALSE, FALSE, sShutdownEvent); - SendEventToServo({{Event::SHUTDOWN}}); + RunOnGLThread([=] {mServo->RequestShutdown();}); log("Waiting for Servo to shutdown"); ::WaitForSingleObject(hEvent, INFINITE); StopRenderLoop(); @@ -132,39 +132,12 @@ void BrowserPage::Loop(cancellation_token cancel) { if (!mAnimating) { ::WaitForSingleObject(hEvent, INFINITE); } - mEventsMutex.lock(); - for (auto &&e : mEvents) { - switch (e.type) { - case Event::CLICK: { - auto [x, y] = e.clickCoords; - mServo->Click(x, y); - break; - } - case Event::SCROLL: { - auto [x, y, dx, dy] = e.scrollCoords; - mServo->Scroll(x, y, dx, dy); - break; - } - case Event::FORWARD: - mServo->GoForward(); - break; - case Event::BACK: - mServo->GoBack(); - break; - case Event::RELOAD: - mServo->Reload(); - break; - case Event::STOP: - mServo->Stop(); - break; - case Event::SHUTDOWN: - log("Requesting Servo to shutdown"); - mServo->RequestShutdown(); - break; - } + mTasksMutex.lock(); + for (auto &&task : mTasks) { + task(); } - mEvents.clear(); - mEventsMutex.unlock(); + mTasks.clear(); + mTasksMutex.unlock(); mServo->PerformUpdates(); } log("Leaving loop"); @@ -271,22 +244,22 @@ template void BrowserPage::RunOnUIThread(Callable cb) { void BrowserPage::OnBackButtonClicked(IInspectable const &, RoutedEventArgs const &) { - SendEventToServo({{Event::BACK}}); + RunOnGLThread([=] { mServo->GoBack(); }); } void BrowserPage::OnForwardButtonClicked(IInspectable const &, RoutedEventArgs const &) { - SendEventToServo({{Event::FORWARD}}); + RunOnGLThread([=] { mServo->GoForward(); }); } void BrowserPage::OnReloadButtonClicked(IInspectable const &, RoutedEventArgs const &) { - SendEventToServo({{Event::RELOAD}}); + RunOnGLThread([=] { mServo->Reload(); }); } void BrowserPage::OnStopButtonClicked(IInspectable const &, RoutedEventArgs const &) { - SendEventToServo({{Event::STOP}}); + RunOnGLThread([=] { mServo->Stop(); }); } void BrowserPage::OnImmersiveButtonClicked(IInspectable const &, @@ -313,9 +286,7 @@ void BrowserPage::OnSurfaceManipulationDelta( auto y = e.Position().Y; auto dx = e.Delta().Translation.X; auto dy = e.Delta().Translation.Y; - Event event = {{Event::SCROLL}}; - event.scrollCoords = {dx, dy, x, y}; - SendEventToServo(event); + RunOnGLThread([=] { mServo->Scroll(x, y, dx, dy); }); e.Handled(true); } @@ -324,14 +295,14 @@ void BrowserPage::OnSurfaceClicked(IInspectable const &, auto coords = e.GetCurrentPoint(swapChainPanel()); auto x = coords.Position().X; auto y = coords.Position().Y; - SendEventToServo({{Event::CLICK}, {x, y}}); + RunOnGLThread([=] { mServo->Click(x, y); }); e.Handled(true); } -void BrowserPage::SendEventToServo(Event event) { - mEventsMutex.lock(); - mEvents.push_back(event); - mEventsMutex.unlock(); +void BrowserPage::RunOnGLThread(std::function task) { + mTasksMutex.lock(); + mTasks.push_back(task); + mTasksMutex.unlock(); WakeUp(); } diff --git a/support/hololens/ServoApp/BrowserPage.h b/support/hololens/ServoApp/BrowserPage.h index efa27ea42153..ac8f017422a4 100644 --- a/support/hololens/ServoApp/BrowserPage.h +++ b/support/hololens/ServoApp/BrowserPage.h @@ -14,12 +14,6 @@ namespace winrt::ServoApp::implementation { static char sWakeupEvent[] = "SIGNAL_WAKEUP"; static char sShutdownEvent[] = "SIGNAL_SHUTDOWN"; -struct Event { - enum { CLICK, SCROLL, BACK, FORWARD, RELOAD, STOP, SHUTDOWN } type; - std::tuple clickCoords; - std::tuple scrollCoords; -}; - struct BrowserPage : BrowserPageT, public servo::ServoDelegate { public: @@ -44,6 +38,7 @@ struct BrowserPage : BrowserPageT, Windows::UI::Xaml::Input::ManipulationDeltaRoutedEventArgs const &e); template void RunOnUIThread(Callable); + void RunOnGLThread(std::function); void Shutdown(); virtual void WakeUp(); @@ -80,9 +75,8 @@ struct BrowserPage : BrowserPageT, EGLSurface mRenderSurface{EGL_NO_SURFACE}; std::unique_ptr mServo; - void BrowserPage::SendEventToServo(Event event); - std::vector mEvents; - std::mutex mEventsMutex; + std::vector> mTasks; + std::mutex mTasksMutex; OpenGLES mOpenGLES; // FIXME: shared pointer diff --git a/support/hololens/ServoApp/Servo.cpp b/support/hololens/ServoApp/Servo.cpp index 5cb35650c724..afe72c2d2af4 100644 --- a/support/hololens/ServoApp/Servo.cpp +++ b/support/hololens/ServoApp/Servo.cpp @@ -1,7 +1,6 @@ #include "pch.h" #include "Servo.h" -// FIXME: rename mozilla to something else namespace servo { void on_load_started() { sServo->Delegate().OnLoadStarted(); }