Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tsmartpointer.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// tcg includes
Toshihiro Shimizu 890ddd
#include "tcg/tcg_list.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tgldisplaylistsmanager.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
//    Local namespace - declarations
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
struct ProxyReference {
Shinya Kitaoka 120a6e
  TGLDisplayListsProxy *m_proxy;
Shinya Kitaoka 120a6e
  int m_refCount;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  ProxyReference(TGLDisplayListsProxy *proxy) : m_proxy(proxy), m_refCount() {}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
//    Local namespace - globals
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tcg::list<proxyreference> m_proxies;</proxyreference>
Toshihiro Shimizu 890ddd
std::map<tglcontext, int=""> m_proxyIdsByContext;</tglcontext,>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
//    TGLDisplayListsManager  implementation
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TGLDisplayListsManager *TGLDisplayListsManager::instance() {
Shinya Kitaoka 120a6e
  static TGLDisplayListsManager theInstance;
Shinya Kitaoka 120a6e
  return &theInstance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TGLDisplayListsManager::storeProxy(TGLDisplayListsProxy *proxy) {
Shinya Kitaoka 120a6e
  return m_proxies.push_back(ProxyReference(proxy));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGLDisplayListsManager::attachContext(int dlSpaceId, TGlContext context) {
Shinya Kitaoka 120a6e
  m_proxyIdsByContext.insert(std::make_pair(context, dlSpaceId));
Shinya Kitaoka 120a6e
  ++m_proxies[dlSpaceId].m_refCount;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TGLDisplayListsManager::releaseContext(TGlContext context) {
Shinya Kitaoka 120a6e
  std::map<tglcontext, int="">::iterator it = m_proxyIdsByContext.find(context);</tglcontext,>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(it != m_proxyIdsByContext.end());
Shinya Kitaoka 120a6e
  if (it == m_proxyIdsByContext.end()) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int dlSpaceId = it->second;
Shinya Kitaoka 120a6e
  if (--m_proxies[dlSpaceId].m_refCount <= 0) {
Shinya Kitaoka 120a6e
    // Notify observers first
Shinya Kitaoka 120a6e
    observers_container::const_iterator ot, oEnd = observers().end();
Shinya Kitaoka 120a6e
    for (ot = observers().begin(); ot != oEnd; ++ot)
Shinya Kitaoka 120a6e
      static_cast<observer *="">(*ot)->onDisplayListDestroyed(dlSpaceId);</observer>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // Then, destroy stuff
Shinya Kitaoka 120a6e
    delete m_proxies[dlSpaceId].m_proxy;
Shinya Kitaoka 120a6e
    m_proxies.erase(dlSpaceId);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_proxyIdsByContext.erase(it);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TGLDisplayListsManager::displayListsSpaceId(TGlContext context) {
Shinya Kitaoka 120a6e
  std::map<tglcontext, int="">::iterator it = m_proxyIdsByContext.find(context);</tglcontext,>
Shinya Kitaoka 120a6e
  return (it == m_proxyIdsByContext.end()) ? -1 : it->second;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TGLDisplayListsProxy *TGLDisplayListsManager::dlProxy(int dlSpaceId) {
Shinya Kitaoka 120a6e
  return m_proxies[dlSpaceId].m_proxy;
Toshihiro Shimizu 890ddd
}