qtcontextmanager.h 3.27 KB
Newer Older
1
2
3
4
#ifndef QTGLCONTEXTMANAGER_H__
#define QTGLCONTEXTMANAGER_H__

#include "tgt/singleton.h"
5
#include "tgt/qt/qtthreadedcanvas.h"
6
#include "tgt/qt/qtglcontext.h"
7
#include "tbb/include/tbb/mutex.h"  // TODO: TBB dependency in TGT is not that beautiful...
8
9
10
11
12
13
14
15
16
#include <map>
#include <string>

namespace tgt {

    /**
     * Manages multiple shared OpenGL contexts and offers methods to ensure that only one context is active at a time.
     * 
     */
17
    class QtContextManager : public Singleton<QtContextManager> {
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    friend class QtGLContext;

    public:
        /**
         * Creates a new QtGLContext for the OpenGL context of \a canvas.
         * \param   canvas  QtCanvas with the OpenGL context to manage
         */
        QtContextManager();

        /**
         * Destructor
         */
        ~QtContextManager();


        /**
34
35
         * Creates a new OpenGL context in a QtThreadedCanvas with the given arguments.
         * Parameters are the same as in QtThreadedCanvas() but context sharing is enables per default.
36
37
38
39
40
41
42
43
         * The newly created context will be active, but the OpenGL mutex not be locked.
         * 
         * \note    The created canvas/context is owned by this ContextManager. Hence, you may not
         *          delete it yourself!
         * 
         * \param   key     Key of the context to create, must be unique.
         * \param   title   Window title
         * \param   size    Window size
44
         * \return  The newly created QtThreadedCanvas.
45
         */
46
        QtThreadedCanvas* createContext(
47
48
49
50
51
52
53
54
55
56
57
58
59
            const std::string& key,
            const std::string& title = "",
            const ivec2& size = ivec2(GLCanvas::DEFAULT_WINDOW_WIDTH, GLCanvas::DEFAULT_WINDOW_HEIGHT),
            const GLCanvas::Buffers buffers = GLCanvas::RGBADD,
            QWidget* parent = 0, bool shared = true, Qt::WFlags f = 0, char* name = 0);
        
        /**
         * Returns the OpenGL context with the given key \a key, 0 if no such context exists.
         * \param   key     Key of the context to return.
         * \return  The OpenGL context with the given key \a key, 0 if no such context exists.
         */
        QtGLContext* getContextByKey(const std::string& key);

60
        tbb::mutex& getGlMutex();
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

        void releaseCurrentContext();
        
    protected:
        /**
         * Sets the given context \a context as the current context for the OpenGL device.
         * If \a context is already the current context, nothing will happen.
         * \param   context     Context to set as current.
         */
        void setCurrent(QtGLContext* context);

        /**
         * Locks the OpenGL device for other threads acessing the ContextManager.
         * \see QtContextManager::release
         */
        void lock();

        /**
         * Releases the lock on the OpenGL device
         * \see QtContextManager::lock
         */
        void unlock();


        std::map<std::string, QtCanvas*> _contexts;  ///< Map of all OpenGL contexts
        QtGLContext* _currentContext;                   ///< Current active OpenGL context
87
        tbb::mutex _glMutex;                                ///< Mutex protecting OpenGL for multi-threaded access
88
89
90
91
92
93
94
95
    };

} // namespace tgt

#define CtxtMgr tgt::Singleton<tgt::QtContextManager>::getRef()

#endif // QTGLCONTEXTMANAGER_H__