qtcontextmanager.cpp 2.72 KB
Newer Older
1
#include "qtcontextmanager.h"
schultezub's avatar
schultezub committed
2

3
#include "tgt/assert.h"
schultezub's avatar
schultezub committed
4
#include "tgt/qt/qtglcontext.h"
5
6
7
8
9
10
11
12
13
14

namespace tgt {

    QtContextManager::QtContextManager()
        : _currentContext(0)
    {
    }

    QtContextManager::~QtContextManager()
    {
schultezub's avatar
schultezub committed
15
        for (std::map<std::string, QtThreadedCanvas*>::iterator it = _contexts.begin(); it != _contexts.end(); ++it) {
16
17
18
19
20
            delete it->second;
        }
        _contexts.clear();
    }

21
    QtThreadedCanvas* QtContextManager::createContext(const std::string& key, const std::string& title /*= ""*/, const ivec2& size /*= ivec2(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT)*/, const GLCanvas::Buffers buffers /*= RGBADD*/, QWidget* parent /*= 0*/, bool shared /*= true*/, Qt::WFlags f /*= 0*/, char* name /*= 0*/)
22
    {
23
24
        // FIXME: rethink this concept of unique IDs
        //tgtAssert(_contexts.find(key) == _contexts.end(), "A context with the same key already exists!");
25

26
        tbb::mutex::scoped_lock lock(_glMutex);
27
        QtThreadedCanvas* toReturn = new QtThreadedCanvas(title, size, buffers, parent, shared, f, name);
28
29
30
        _contexts.insert(std::make_pair(key, toReturn));

        toReturn->makeCurrent();
31
        // Init GLEW for this context
32
33
34
35
36
37
38
39
40
41
42
43
        GLenum err = glewInit();
        if (err != GLEW_OK) {
            // Problem: glewInit failed, something is seriously wrong.
            tgtAssert(false, "glewInit failed");
            std::cerr << "glewInit failed, error: " << glewGetErrorString(err) << std::endl;
            exit(EXIT_FAILURE);
        }

        return toReturn;
    }

    QtGLContext* QtContextManager::getContextByKey(const std::string& key) {
schultezub's avatar
schultezub committed
44
        std::map<std::string, QtThreadedCanvas*>::iterator it = _contexts.find(key);
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        if (it != _contexts.end())
            return it->second->getContext();
        else
            return 0;
    }

    void QtContextManager::setCurrent(QtGLContext* context) {
        if (_currentContext != context) {
            if (context == 0) {
                // explicitely release OpenGL context
                _currentContext->getCanvas()->doneCurrent();
                _currentContext = 0;
            }
            else {
                context->getCanvas()->makeCurrent();
                LGL_ERROR;
                _currentContext = context;
            }
        }
    }

    void QtContextManager::lock() {
        _glMutex.lock();
    }

    void QtContextManager::unlock() {
71
        releaseCurrentContext();
72
73
74
        _glMutex.unlock();
    }

75
    tbb::mutex& QtContextManager::getGlMutex() {
76
77
78
79
80
81
82
83
        return _glMutex;
    }

    void QtContextManager::releaseCurrentContext() {
        glFlush();
        setCurrent(0);
    }

84
85
86
87
88
    QtCanvas* QtContextManager::getCurrentContext() const {
        return _currentContext->getCanvas();
    }


89
}