| 98 | } |
| 99 | |
| 100 | OSGLContextPtr |
| 101 | GPUContextPool::attachGLContextToRender(bool checkIfGLLoaded) |
| 102 | { |
| 103 | if (checkIfGLLoaded && (!appPTR->isOpenGLLoaded() || !appPTR->getCurrentSettings()->isOpenGLRenderingEnabled())) { |
| 104 | return OSGLContextPtr(); |
| 105 | } |
| 106 | QMutexLocker k(&_imp->contextPoolMutex); |
| 107 | |
| 108 | // Context-sharing disabled as it is not needed |
| 109 | OSGLContextPtr shareContext;// _imp->glShareContext.lock(); |
| 110 | OSGLContextPtr newContext; |
| 111 | SettingsPtr settings = appPTR->getCurrentSettings(); |
| 112 | GLRendererID rendererID; |
| 113 | if (settings) { |
| 114 | rendererID = settings->getActiveOpenGLRendererID(); |
| 115 | } |
| 116 | |
| 117 | int maxContexts = settings ? std::max(settings->getMaxOpenGLContexts(), 1) : 1; |
| 118 | |
| 119 | #ifndef NATRON_RENDER_SHARED_CONTEXT |
| 120 | while (_imp->glContextPool.empty() && (int)_imp->attachedGLContexts.size() >= maxContexts) { |
| 121 | _imp->glContextPoolEmpty.wait(k.mutex()); |
| 122 | } |
| 123 | if ( _imp->glContextPool.empty() ) { |
| 124 | assert( (int)_imp->attachedGLContexts.size() < maxContexts ); |
| 125 | // Create a new one |
| 126 | newContext = std::make_shared<OSGLContext>( FramebufferConfig(), shareContext.get(), GLVersion.major, GLVersion.minor, rendererID ); |
| 127 | } else { |
| 128 | std::set<OSGLContextPtr>::iterator it = _imp->glContextPool.begin(); |
| 129 | newContext = *it; |
| 130 | assert(newContext); |
| 131 | _imp->glContextPool.erase(it); |
| 132 | } |
| 133 | #else |
| 134 | |
| 135 | if ( (int)_imp->glContextPool.size() < maxContexts ) { |
| 136 | // Create a new one |
| 137 | newContext = std::make_shared<OSGLContext>( FramebufferConfig(), shareContext.get(), GLVersion.major, GLVersion.minor, rendererID ); |
| 138 | _imp->glContextPool.insert(newContext); |
| 139 | } else { |
| 140 | while ((int)_imp->glContextPool.size() > maxContexts) { |
| 141 | _imp->glContextPool.erase(_imp->glContextPool.begin()); |
| 142 | } |
| 143 | |
| 144 | // Cycle through all contexts for all renders |
| 145 | OSGLContextPtr lastContext = _imp->lastUsedGLContext.lock(); |
| 146 | if (!lastContext) { |
| 147 | newContext = *_imp->glContextPool.begin(); |
| 148 | } else { |
| 149 | std::set<OSGLContextPtr>::iterator foundLast = _imp->glContextPool.find(lastContext); |
| 150 | assert( foundLast != _imp->glContextPool.end() ); |
| 151 | if ( foundLast == _imp->glContextPool.end() ) { |
| 152 | throw std::logic_error("No context to attach"); |
| 153 | } else { |
| 154 | std::set<OSGLContextPtr>::iterator next = foundLast; |
| 155 | ++next; |
| 156 | if ( next == _imp->glContextPool.end() ) { |
| 157 | next = _imp->glContextPool.begin(); |
no test coverage detected