[use] use DrapePointers everywhere

This commit is contained in:
ExMix 2013-12-23 11:22:56 +03:00 committed by Alex Zolotarev
parent 8c7a950079
commit 3d7aaa706a
16 changed files with 115 additions and 97 deletions

View file

@ -8,27 +8,27 @@
namespace df
{
BackendRenderer::BackendRenderer(ThreadsCommutator * commutator,
BackendRenderer::BackendRenderer(RefPointer<ThreadsCommutator> commutator,
double visualScale,
int surfaceWidth,
int surfaceHeight)
: m_commutator(commutator)
{
m_impl = new BackendRendererImpl(commutator, visualScale, surfaceWidth, surfaceHeight);
m_post = bind(&ThreadsCommutator::PostMessage, commutator, ThreadsCommutator::ResourceUploadThread, _1);
m_impl.Reset(new BackendRendererImpl(m_commutator, visualScale, surfaceWidth, surfaceHeight));
}
BackendRenderer::~BackendRenderer()
{
delete m_impl;
m_impl.Destroy();
}
void BackendRenderer::UpdateCoverage(const ScreenBase & screen)
{
m_post(new UpdateCoverageMessage(screen));
m_commutator->PostMessage(ThreadsCommutator::ResourceUploadThread, MovePointer<Message>(new UpdateCoverageMessage(screen)));
}
void BackendRenderer::Resize(int x0, int y0, int w, int h)
{
m_post(new ResizeMessage(x0, y0, w, h));
m_commutator->PostMessage(ThreadsCommutator::ResourceUploadThread, MovePointer<Message>(new ResizeMessage(x0, y0, w, h)));
}
}

View file

@ -1,5 +1,7 @@
#pragma once
#include "../drape/pointers.hpp"
#include "../geometry/screenbase.hpp"
#include "../std/function.hpp"
@ -10,12 +12,10 @@ namespace df
class BackendRendererImpl;
class Message;
typedef function<void (Message *)> post_message_fn_t;
class BackendRenderer
{
public:
BackendRenderer(ThreadsCommutator * commutator,
BackendRenderer(RefPointer<ThreadsCommutator> commutator,
double visualScale,
int surfaceWidth,
int surfaceHeight);
@ -26,7 +26,7 @@ namespace df
void Resize(int x0, int y0, int w, int h);
private:
BackendRendererImpl * m_impl;
post_message_fn_t m_post;
MasterPointer<BackendRendererImpl> m_impl;
RefPointer<ThreadsCommutator> m_commutator;
};
}

View file

@ -18,7 +18,7 @@ namespace df
const GLState & state,
TransferPointer<VertexArrayBuffer> buffer)
{
sendMessage(new FlushTileMessage(key, state, buffer));
sendMessage(MovePointer<Message>(new FlushTileMessage(key, state, buffer)));
}
}
@ -26,7 +26,7 @@ namespace df
: m_sendMessageFn(sendMessageFn)
{
for (int i = 0; i < initBatcherCount; ++i)
m_batchers.push(new Batcher());
m_batchers.push(MasterPointer<Batcher>(new Batcher()));
}
BatchersPool::~BatchersPool()
@ -34,33 +34,34 @@ namespace df
ASSERT(m_reservedBatchers.empty(), ());
while (!m_batchers.empty())
{
Batcher * batcher = m_batchers.top();
delete batcher;
m_batchers.top().Destroy();
m_batchers.pop();
}
}
void BatchersPool::AcceptMessage(Message * message)
void BatchersPool::AcceptMessage(RefPointer<Message> message)
{
switch (message->GetType())
{
case Message::TileReadStarted:
{
BaseTileMessage * msg = static_cast<BaseTileMessage *>(message);
BaseTileMessage * msg = static_cast<BaseTileMessage *>(message.GetRaw());
ReserveBatcher(msg->GetKey());
break;
}
case Message::TileReadEnded:
{
BaseTileMessage * msg = static_cast<BaseTileMessage *>(message);
BaseTileMessage * msg = static_cast<BaseTileMessage *>(message.GetRaw());
ReleaseBatcher(msg->GetKey());
break;
}
case Message::MapShapeReaded:
{
MapShapeReadedMessage * mapShape = static_cast<MapShapeReadedMessage *>(message);
Batcher * b = GetTileBatcher(mapShape->GetKey());
mapShape->GetShape()->Draw(b);
MapShapeReadedMessage * mapShapeMessage = static_cast<MapShapeReadedMessage *>(message.GetRaw());
RefPointer<Batcher> b = GetTileBatcher(mapShapeMessage->GetKey());
MasterPointer<MapShape> shape(mapShapeMessage->GetShape());
shape->Draw(b);
shape.Destroy();
break;
}
default:
@ -71,9 +72,9 @@ namespace df
void BatchersPool::ReserveBatcher(TileKey const & key)
{
Batcher * reserved = NULL;
MasterPointer<Batcher> reserved;
if (m_batchers.empty())
reserved = new Batcher();
reserved.Reset(new Batcher());
else
{
reserved = m_batchers.top();
@ -84,12 +85,12 @@ namespace df
VERIFY(m_reservedBatchers.insert(make_pair(key, reserved)).second, ());
}
Batcher * BatchersPool::GetTileBatcher(TileKey const & key)
RefPointer<Batcher> BatchersPool::GetTileBatcher(TileKey const & key)
{
reserved_batchers_t::iterator it = m_reservedBatchers.find(key);
ASSERT(it != m_reservedBatchers.end(), ());
return it->second;
return it->second.GetRefPointer();
}
void BatchersPool::ReleaseBatcher(TileKey const & key)
@ -97,7 +98,7 @@ namespace df
reserved_batchers_t::iterator it = m_reservedBatchers.find(key);
ASSERT(it != m_reservedBatchers.end(), ());
Batcher * batcher = it->second;
MasterPointer<Batcher> batcher = it->second;
batcher->EndSession();
m_reservedBatchers.erase(it);
m_batchers.push(batcher);

View file

@ -2,6 +2,8 @@
#include "tile_info.hpp"
#include "../drape/pointers.hpp"
#include "../std/map.hpp"
#include "../std/stack.hpp"
#include "../std/function.hpp"
@ -14,21 +16,21 @@ namespace df
class BatchersPool
{
public:
typedef function<void (Message *)> send_message_fn;
typedef function<void (TransferPointer<Message>)> send_message_fn;
BatchersPool(int initBatcherCount, const send_message_fn & sendMessageFn);
~BatchersPool();
void AcceptMessage(Message * message);
void AcceptMessage(RefPointer<Message> message);
private:
void ReserveBatcher(TileKey const & key);
Batcher * GetTileBatcher(TileKey const & key);
RefPointer<Batcher> GetTileBatcher(TileKey const & key);
void ReleaseBatcher(TileKey const & key);
private:
stack<Batcher *> m_batchers;
typedef map<TileKey, Batcher *> reserved_batchers_t;
stack<MasterPointer<Batcher> > m_batchers;
typedef map<TileKey, MasterPointer<Batcher> > reserved_batchers_t;
reserved_batchers_t m_reservedBatchers;
send_message_fn m_sendMessageFn;

View file

@ -5,7 +5,7 @@
namespace df
{
EngineContext::EngineContext(ThreadsCommutator * commutator)
EngineContext::EngineContext(RefPointer<ThreadsCommutator> commutator)
: m_commutator(commutator)
{
}
@ -15,7 +15,7 @@ namespace df
PostMessage(new TileReadStartMessage(key));
}
void EngineContext::InsertShape(TileKey const & key, MapShape const * shape)
void EngineContext::InsertShape(TileKey const & key, TransferPointer<MapShape> shape)
{
PostMessage(new MapShapeReadedMessage(key, shape));
}
@ -27,6 +27,6 @@ namespace df
void EngineContext::PostMessage(Message * message)
{
m_commutator->PostMessage(ThreadsCommutator::ResourceUploadThread, message);
m_commutator->PostMessage(ThreadsCommutator::ResourceUploadThread, MovePointer(message));
}
}

View file

@ -3,6 +3,8 @@
#include "tile_info.hpp"
#include "threads_commutator.hpp"
#include "../drape/pointers.hpp"
namespace df
{
class Message;
@ -11,18 +13,18 @@ namespace df
class EngineContext
{
public:
EngineContext(ThreadsCommutator * commutator);
EngineContext(RefPointer<ThreadsCommutator> commutator);
void BeginReadTile(TileKey const & key);
/// If you call this method, you may forget about shape.
/// It will be proccessed and delete later
void InsertShape(TileKey const & key, MapShape const * shape);
void InsertShape(TileKey const & key, TransferPointer<MapShape> shape);
void EndReadTile(TileKey const & key);
private:
void PostMessage(Message * message);
private:
ThreadsCommutator * m_commutator;
RefPointer<ThreadsCommutator> m_commutator;
};
}

View file

@ -20,9 +20,9 @@
namespace
{
void PostFinishTask(df::ThreadsCommutator * commutator, threads::IRoutine * routine)
void PostFinishTask(RefPointer<df::ThreadsCommutator> commutator, threads::IRoutine * routine)
{
commutator->PostMessage(df::ThreadsCommutator::ResourceUploadThread, new df::TaskFinishMessage(routine));
commutator->PostMessage(df::ThreadsCommutator::ResourceUploadThread, MovePointer<df::Message>(new df::TaskFinishMessage(routine)));
}
struct CoverageCellComparer
@ -43,7 +43,7 @@ namespace
namespace df
{
BackendRendererImpl::BackendRendererImpl(ThreadsCommutator * commutator,
BackendRendererImpl::BackendRendererImpl(RefPointer<ThreadsCommutator> commutator,
double visualScale,
int surfaceWidth,
int surfaceHeight)
@ -55,8 +55,8 @@ namespace df
m_commutator->RegisterThread(ThreadsCommutator::ResourceUploadThread, this);
int readerCount = max(1, GetPlatform().CpuCores() - 2);
m_threadPool = new threads::ThreadPool(readerCount, bind(&PostFinishTask, commutator, _1));
m_batchersPool = new BatchersPool(readerCount, bind(&BackendRendererImpl::PostToRenderThreads, this, _1));
m_threadPool.Reset(new threads::ThreadPool(readerCount, bind(&PostFinishTask, commutator, _1)));
m_batchersPool.Reset(new BatchersPool(readerCount, bind(&BackendRendererImpl::PostToRenderThreads, this, _1)));
StartThread();
}
@ -81,7 +81,7 @@ namespace df
CancelTask(*it, false, false);
m_taskIndex.clear();
PostToRenderThreads(new DropCoverageMessage());
PostToRenderThreads(MovePointer<Message>(new DropCoverageMessage()));
for (size_t i = 0; i < tiles.size(); ++i)
CreateTask(tiles[i]);
@ -144,7 +144,7 @@ namespace df
task->Cancel();
if (postToRenderer)
PostToRenderThreads(new DropTileMessage(task->GetTileInfo().m_key));
PostToRenderThreads(MovePointer<Message>(new DropTileMessage(task->GetTileInfo().m_key)));
if (removefromIndex)
m_taskIndex.erase(task);
@ -167,18 +167,18 @@ namespace df
/////////////////////////////////////////
// MessageAcceptor //
/////////////////////////////////////////
void BackendRendererImpl::AcceptMessage(Message * message)
void BackendRendererImpl::AcceptMessage(RefPointer<Message> message)
{
switch (message->GetType())
{
case Message::UpdateCoverage:
UpdateCoverage(static_cast<UpdateCoverageMessage *>(message)->GetScreen());
UpdateCoverage(static_cast<UpdateCoverageMessage *>(message.GetRaw())->GetScreen());
break;
case Message::Resize:
Resize(static_cast<ResizeMessage *>(message)->GetRect());
Resize(static_cast<ResizeMessage *>(message.GetRaw())->GetRect());
break;
case Message::TaskFinish:
FinishTask(static_cast<TaskFinishMessage *>(message)->GetRoutine());
FinishTask(static_cast<TaskFinishMessage *>(message.GetRaw())->GetRoutine());
break;
case Message::TileReadStarted:
case Message::TileReadEnded:
@ -219,14 +219,10 @@ namespace df
void BackendRendererImpl::ReleaseResources()
{
m_threadPool->Stop();
delete m_threadPool;
m_threadPool.Destroy();
m_batchersPool.Destroy();
delete m_batchersPool;
typedef set<ReadMWMTask *>::iterator index_iter;
for_each(m_taskIndex.begin(), m_taskIndex.end(), DeleteFunctor());
m_taskIndex.clear();
GetRangeDeletor(m_taskIndex, DeleteFunctor())();
}
void BackendRendererImpl::Do()
@ -234,7 +230,7 @@ namespace df
ThreadMain();
}
void BackendRendererImpl::PostToRenderThreads(Message * message)
void BackendRendererImpl::PostToRenderThreads(TransferPointer<Message> message)
{
m_commutator->PostMessage(ThreadsCommutator::RenderThread, message);
}

View file

@ -5,6 +5,8 @@
#include "engine_context.hpp"
#include "batchers_pool.hpp"
#include "../drape/pointers.hpp"
#include "../map/scales_processor.hpp"
#include "../map/tiler.hpp"
@ -25,7 +27,7 @@ namespace df
public threads::IRoutine
{
public:
BackendRendererImpl(ThreadsCommutator * commutator,
BackendRendererImpl(RefPointer<ThreadsCommutator> commutator,
double visualScale,
int surfaceWidth,
int surfaceHeight);
@ -59,14 +61,14 @@ namespace df
/// transfer it to batchers
MemoryFeatureIndex m_index;
EngineContext m_engineContext;
BatchersPool * m_batchersPool;
MasterPointer<BatchersPool> m_batchersPool;
/////////////////////////////////////////
/////////////////////////////////////////
// MessageAcceptor //
/////////////////////////////////////////
private:
void AcceptMessage(Message * message);
void AcceptMessage(RefPointer<Message> message);
/////////////////////////////////////////
// ThreadPart //
@ -78,11 +80,11 @@ namespace df
void ReleaseResources();
virtual void Do();
void PostToRenderThreads(Message * message);
void PostToRenderThreads(TransferPointer<Message> message);
private:
threads::Thread m_selfThread;
threads::ThreadPool * m_threadPool;
ThreadsCommutator * m_commutator;
MasterPointer<threads::ThreadPool> m_threadPool;
RefPointer<ThreadsCommutator> m_commutator;
};
}

View file

@ -3,6 +3,8 @@
#include "message.hpp"
#include "tile_info.hpp"
#include "../drape/pointers.hpp"
class Batcher;
namespace df
@ -11,13 +13,13 @@ namespace df
{
public:
virtual ~MapShape(){}
virtual void Draw(Batcher * batcher) const = 0;
virtual void Draw(RefPointer<Batcher> batcher) const = 0;
};
class MapShapeReadedMessage : public Message
{
public:
MapShapeReadedMessage(const TileKey & key, MapShape const * shape)
MapShapeReadedMessage(const TileKey & key, TransferPointer<MapShape> shape)
: m_key(key), m_shape(shape)
{
SetType(MapShapeReaded);
@ -25,14 +27,15 @@ namespace df
~MapShapeReadedMessage()
{
delete m_shape;
m_shape.Destroy();
}
const TileKey & GetKey() const { return m_key; }
MapShape const * GetShape() const { return m_shape; }
/// return non const reference for correct construct MasterPointer
TransferPointer<MapShape> & GetShape() { return m_shape; }
private:
TileKey m_key;
MapShape const * m_shape;
TransferPointer<MapShape> m_shape;
};
}

View file

@ -6,15 +6,16 @@ namespace df
{
void MessageAcceptor::ProcessSingleMessage(bool waitMessage)
{
Message * message = m_messageQueue.PopMessage(waitMessage);
if (message == NULL)
TransferPointer<Message> transferMessage = m_messageQueue.PopMessage(waitMessage);
MasterPointer<Message> message(transferMessage);
if (message.IsNull())
return;
AcceptMessage(message);
delete message;
AcceptMessage(message.GetRefPointer());
message.Destroy();
}
void MessageAcceptor::PostMessage(Message * message)
void MessageAcceptor::PostMessage(TransferPointer<Message> message)
{
m_messageQueue.PushMessage(message);
}
@ -22,13 +23,6 @@ namespace df
void MessageAcceptor::CloseQueue()
{
m_messageQueue.CancelWait();
ClearQueue();
}
void MessageAcceptor::ClearQueue()
{
Message * message;
while ((message = m_messageQueue.PopMessage(false)) != NULL)
delete message;
m_messageQueue.ClearQuery();
}
}

View file

@ -2,6 +2,8 @@
#include "message_queue.hpp"
#include "../drape/pointers.hpp"
namespace df
{
class Message;
@ -9,7 +11,7 @@ namespace df
class MessageAcceptor
{
protected:
virtual void AcceptMessage(Message * message) = 0;
virtual void AcceptMessage(RefPointer<Message> message) = 0;
/// Must be called by subclass on message target thread
void ProcessSingleMessage(bool waitMessage);
@ -18,8 +20,7 @@ namespace df
private:
friend class ThreadsCommutator;
void PostMessage(Message * message);
void ClearQueue();
void PostMessage(TransferPointer<Message> message);
private:
MessageQueue m_messageQueue;

View file

@ -1,10 +1,17 @@
#include "message_queue.hpp"
#include "../base/assert.hpp"
#include "../base/stl_add.hpp"
namespace df
{
Message * MessageQueue::PopMessage(bool waitMessage)
MessageQueue::~MessageQueue()
{
CancelWait();
ClearQuery();
}
TransferPointer<Message> MessageQueue::PopMessage(bool waitMessage)
{
threads::ConditionGuard guard(m_condition);
@ -14,19 +21,19 @@ namespace df
/// even waitNonEmpty == true m_messages can be empty after WaitMessage call
/// if application preparing to close and CancelWait been called
if (m_messages.empty())
return NULL;
return MasterPointer<Message>(NULL).Move();
Message * msg = m_messages.front();
MasterPointer<Message> msg = m_messages.front();
m_messages.pop_front();
return msg;
return msg.Move();
}
void MessageQueue::PushMessage(Message * message)
void MessageQueue::PushMessage(TransferPointer<Message> message)
{
threads::ConditionGuard guard(m_condition);
bool wasEmpty = m_messages.empty();
m_messages.push_back(message);
m_messages.push_back(MasterPointer<Message>(message));
if (wasEmpty)
guard.Signal();
@ -42,4 +49,9 @@ namespace df
{
m_condition.Signal();
}
void MessageQueue::ClearQuery()
{
GetRangeDeletor(m_messages, MasterPointerDeleter())();
}
}

View file

@ -2,6 +2,8 @@
#include "message.hpp"
#include "../drape/pointers.hpp"
#include "../base/condition.hpp"
#include "../std/list.hpp"
@ -11,18 +13,19 @@ namespace df
class MessageQueue
{
public:
MessageQueue();
~MessageQueue();
/// if queue is empty than return NULL
Message * PopMessage(bool waitMessage);
void PushMessage(Message * message);
TransferPointer<Message> PopMessage(bool waitMessage);
void PushMessage(TransferPointer<Message> message);
void CancelWait();
void ClearQuery();
private:
void WaitMessage();
private:
threads::Condition m_condition;
list<Message *> m_messages;
list<MasterPointer<Message> > m_messages;
};
}

View file

@ -67,6 +67,7 @@ namespace df
}
const GLState & GetState() const { return m_state; }
TransferPointer<VertexArrayBuffer> & GetBuffer();
private:

View file

@ -11,12 +11,12 @@ namespace df
VERIFY(m_acceptors.insert(make_pair(name, acceptor)).second, ());
}
void ThreadsCommutator::PostMessage(ThreadName name, Message * message)
void ThreadsCommutator::PostMessage(ThreadName name, TransferPointer<Message> message)
{
acceptors_map_t::iterator it = m_acceptors.find(name);
ASSERT(it != m_acceptors.end(), ());
if (it != m_acceptors.end())
it->second->AcceptMessage(message);
it->second->PostMessage(message);
}
}

View file

@ -1,5 +1,6 @@
#pragma once
#include "../drape/pointers.hpp"
#include "../std/map.hpp"
namespace df
@ -16,8 +17,8 @@ namespace df
ResourceUploadThread
};
void RegisterThread(ThreadName name, MessageAcceptor * acceptor);
void PostMessage(ThreadName name, Message * message);
void RegisterThread(ThreadName name, MessageAcceptor *acceptor);
void PostMessage(ThreadName name, TransferPointer<Message> message);
private:
typedef map<ThreadName, MessageAcceptor *> acceptors_map_t;