glcontextmanager.cpp 2.65 KB
Newer Older
1
2
3
4
5
#include "glcontextmanager.h"

#include "tgt/assert.h"

namespace tgt {
6
    
7
8
9
10
11
12
13
    GlContextManager::GlContextManager()
        : _currentContext(0)
    {
    }

    GlContextManager::~GlContextManager()
    {
14
15
        for (std::set<GLCanvas*>::iterator it = _contexts.begin(); it != _contexts.end(); ++it) {
            delete *it;
16
17
18
19
20
21
22
23
24
25
26
27
        }
        _contexts.clear();
    }

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

    void GlContextManager::unlock() {
        _glMutex.unlock();
    }

28
    std::mutex& GlContextManager::getGlMutex() {
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        return _glMutex;
    }

    void GlContextManager::releaseCurrentContext() {
        setCurrent(0);
    }

    GLCanvas* GlContextManager::getCurrentContext() const {
        return _currentContext;
    }

    void GlContextManager::lockAndAcquire(GLCanvas* context) {
        lock();
        setCurrent(context);
    }

    void GlContextManager::releaseAndUnlock() {
        releaseCurrentContext();
        unlock();
    }

    void GlContextManager::acquireContext(GLCanvas* context) {
        setCurrent(context);
    }

54
55
56
57
58
    void GlContextManager::registerContextAndInitGlew(GLCanvas* context) {
        tgtAssert(context != 0, "Given context must not be 0.");
        tgtAssert(_contexts.find(context) == _contexts.end(), "Tried to double register the same context.");

        {
59
            std::lock_guard<std::mutex> localLock(_localMutex);
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
            _contexts.insert(context);
        }
       
        // Init GLEW for this context
        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);
        }

        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
    }

76
77
78
    void GlContextManager::removeContext(GLCanvas* context) {
        _currentContext = 0;

79
        std::lock_guard<std::mutex> lock(_localMutex);
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
        std::set<GLCanvas*>::iterator it = _contexts.find(context);
        if (it != _contexts.end()) {
            _contexts.erase(it);
        }
    }

    void GlContextManager::setCurrent(GLCanvas* context) {
        if (_currentContext != context) {
            if (context == 0) {
                // explicitely release OpenGL context
                _currentContext->releaseAsCurrentContext();
                _currentContext = 0;
            }
            else {
                context->acquireAsCurrentContext();
                LGL_ERROR;
                _currentContext = context;
97
98
            }
        }
99

100
    }
101
102

}