Skip to content

Commit

Permalink
Added synchronous stopping of render threads
Browse files Browse the repository at this point in the history
  • Loading branch information
r.kuznetsov committed Nov 30, 2015
1 parent d7b1edc commit 404c904
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
8 changes: 6 additions & 2 deletions drape_frontend/backend_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ BackendRenderer::BackendRenderer(dp::RefPointer<ThreadsCommutator> commutator,
dp::RefPointer<dp::OGLContextFactory> oglcontextfactory,
dp::RefPointer<dp::TextureManager> textureManager,
MapDataProvider const & model)
: BaseRenderer(commutator, oglcontextfactory)
: BaseRenderer(ThreadsCommutator::ResourceUploadThread, commutator, oglcontextfactory)
, m_model(model)
, m_engineContext(commutator)
, m_texturesManager(textureManager)
, m_guiCacher("default")
{
m_commutator->RegisterThread(ThreadsCommutator::ResourceUploadThread, this);
m_batchersPool.Reset(new BatchersPool(ReadManager::ReadCount(), bind(&BackendRenderer::FlushGeometry, this, _1)));
m_readManager.Reset(new ReadManager(m_engineContext, m_model));

Expand Down Expand Up @@ -99,6 +98,11 @@ void BackendRenderer::AcceptMessage(dp::RefPointer<Message> message)
m_batchersPool->ReleaseBatcher(key);
break;
}
case Message::StopRendering:
{
ProcessStopRenderingMessage();
break;
}
default:
ASSERT(false, ());
break;
Expand Down
25 changes: 18 additions & 7 deletions drape_frontend/base_renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#include "base_renderer.hpp"
#include "threads_commutator.hpp"
#include "message_subclasses.hpp"
#include "../std/utility.hpp"

#include "../base/logging.hpp"


namespace df
{

BaseRenderer::BaseRenderer(dp::RefPointer<ThreadsCommutator> commutator,
BaseRenderer::BaseRenderer(ThreadsCommutator::ThreadName name,
dp::RefPointer<ThreadsCommutator> commutator,
dp::RefPointer<dp::OGLContextFactory> oglcontextfactory)
: m_commutator(commutator)
, m_contextFactory(oglcontextfactory)
, m_threadName(name)
, m_isEnabled(true)
, m_renderingEnablingCompletionHandler(nullptr)
, m_wasNotified(false)
{
m_commutator->RegisterThread(m_threadName, this);
}

void BaseRenderer::StartThread()
Expand All @@ -25,13 +25,18 @@ void BaseRenderer::StartThread()

void BaseRenderer::StopThread()
{
IRoutine::Cancel();
// send message to stop rendering in render thread
m_commutator->PostMessage(m_threadName,
dp::MovePointer<Message>(new StopRenderingMessage()),
MessagePriority::High);

// wake up render thread if necessary
if (!m_isEnabled)
{
WakeUp();
}
CloseQueue();

// wait for render thread completion
m_selfThread.Join();
}

Expand Down Expand Up @@ -116,4 +121,10 @@ void BaseRenderer::WakeUp()
m_renderingEnablingCondition.notify_one();
}

void BaseRenderer::ProcessStopRenderingMessage()
{
CloseQueue();
IRoutine::Cancel();
}

} // namespace df
9 changes: 6 additions & 3 deletions drape_frontend/base_renderer.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "message_acceptor.hpp"
#include "threads_commutator.hpp"
#include "../base/thread.hpp"
#include "../drape/oglcontextfactory.hpp"

Expand All @@ -12,15 +13,14 @@
namespace df
{

class ThreadsCommutator;

class BaseRenderer : public MessageAcceptor,
public threads::IRoutine
{
public:
using TCompletionHandler = function<void()>;

BaseRenderer(dp::RefPointer<ThreadsCommutator> commutator,
BaseRenderer(ThreadsCommutator::ThreadName name,
dp::RefPointer<ThreadsCommutator> commutator,
dp::RefPointer<dp::OGLContextFactory> oglcontextfactory);

void SetRenderingEnabled(bool const isEnabled);
Expand All @@ -32,10 +32,13 @@ class BaseRenderer : public MessageAcceptor,

void StartThread();
void StopThread();

void CheckRenderingEnabled();
void ProcessStopRenderingMessage();

private:
threads::Thread m_selfThread;
ThreadsCommutator::ThreadName m_threadName;

mutex m_renderingEnablingMutex;
condition_variable m_renderingEnablingCondition;
Expand Down
10 changes: 7 additions & 3 deletions drape_frontend/frontend_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ FrontendRenderer::FrontendRenderer(dp::RefPointer<ThreadsCommutator> commutator,
dp::RefPointer<dp::OGLContextFactory> oglcontextfactory,
dp::RefPointer<dp::TextureManager> textureManager,
Viewport viewport)
: BaseRenderer(commutator, oglcontextfactory)
: BaseRenderer(ThreadsCommutator::RenderThread, commutator, oglcontextfactory)
, m_textureManager(textureManager)
, m_gpuProgramManager(new dp::GpuProgramManager())
, m_viewport(viewport)
Expand All @@ -46,10 +46,9 @@ FrontendRenderer::FrontendRenderer(dp::RefPointer<ThreadsCommutator> commutator,
m_fps = 0.0;
#endif

m_commutator->RegisterThread(ThreadsCommutator::RenderThread, this);

RefreshProjection();
RefreshModelView();

StartThread();
}

Expand Down Expand Up @@ -221,6 +220,11 @@ void FrontendRenderer::AcceptMessage(dp::RefPointer<Message> message)
m_guiRenderer->Build(m_gpuProgramManager.GetRefPointer());
break;
}
case Message::StopRendering:
{
ProcessStopRenderingMessage();
break;
}
default:
ASSERT(false, ());
}
Expand Down
3 changes: 2 additions & 1 deletion drape_frontend/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Message
ClearUserMarkLayer,
ChangeUserMarkLayerVisibility,
UpdateUserMarkLayer,
GuiLayerRecached
GuiLayerRecached,
StopRendering
};

virtual ~Message() {}
Expand Down
7 changes: 7 additions & 0 deletions drape_frontend/message_subclasses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,11 @@ class GuiLayerRecachedMessage : public Message
dp::TransferPointer<gui::LayerRenderer> m_renderer;
};

class StopRenderingMessage : public Message
{
public:
StopRenderingMessage(){}
Type GetType() const override { return Message::StopRendering; }
};

} // namespace df

0 comments on commit 404c904

Please sign in to comment.