2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

glcontextmanager.cpp 2.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "glcontextmanager.h"

#include "tgt/assert.h"

namespace tgt {

    GlContextManager* GlContextManager::singletonClass_ = 0;

    GlContextManager::GlContextManager()
        : _currentContext(0)
    {
    }

    GlContextManager::~GlContextManager()
    {
        for (std::map<std::string, GLCanvas*>::iterator it = _contexts.begin(); it != _contexts.end(); ++it) {
            delete it->second;
        }
        _contexts.clear();
    }


    GLCanvas* GlContextManager::getContextByKey(const std::string& key) {
24
        tbb::mutex::scoped_lock lock(_localMutex);
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
        std::map<std::string, GLCanvas*>::iterator it = _contexts.find(key);
        if (it != _contexts.end())
            return it->second;
        else
            return 0;
    }

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

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

    tbb::mutex& GlContextManager::getGlMutex() {
        return _glMutex;
    }

    void GlContextManager::releaseCurrentContext() {
45
        if (_currentContext != 0)
46
            glFinish();
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        setCurrent(0);
    }

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

    GlContextManager* GlContextManager::getPtr() {
        tgtAssert( singletonClass_ != 0, "singletonClass_ has not been intitialized." );
        return singletonClass_;
    }

    GlContextManager& GlContextManager::getRef() {
        tgtAssert( singletonClass_ != 0 , "singletonClass_ has not been intitialized." );
        return *singletonClass_;
    }

    bool GlContextManager::isInited() {
        return (singletonClass_ != 0);
    }

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

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

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

82
83
84
85
86
87
88
89
90
91
92
    void GlContextManager::removeContext(GLCanvas* context) {
        _currentContext = 0;

        tbb::mutex::scoped_lock lock(_localMutex);
        for (std::map<std::string, GLCanvas*>::iterator it = _contexts.begin(); it != _contexts.end(); ++it) {
            if (it->second == context) {
                _contexts.erase(it);
                break;
            }
        }
    }
93
94

}