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

opengljobprocessor.h 7.3 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
24
25
26
27
28
/**********************************************************************
 *                                                                    *
 * cgt - CAMP Graphics Toolbox, Copyright (C) 2012-2014               *
 *     Chair for Computer Aided Medical Procedures                    *
 *     Technische Universitaet Muenchen, Germany.                     *
 *     <http://campar.in.tum.de/>                                     *
 *                                                                    *
 * forked from tgt - Tiny Graphics Toolbox, Copyright (C) 2006-2011   *
 *     Visualization and Computer Graphics Group, Department of       *
 *     Computer Science, University of Muenster, Germany.             *
 *     <http://viscg.uni-muenster.de>                                 *
 *                                                                    *
 * This file is part of the cgt library. This library is free         *
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the       *
 * GNU Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

29
30
// ================================================================================================
// 
schultezub's avatar
schultezub committed
31
// This file is part of the CAMPVis Software Framework.
32
// 
33
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
schultezub's avatar
schultezub committed
34
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
35
//      Chair for Computer Aided Medical Procedures
36
37
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
38
// 
schultezub's avatar
schultezub committed
39
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
40
// 
41
42
43
44
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
45
// 
46
47
48
49
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
50
51
52
53
54
55
// 
// ================================================================================================

#ifndef OPENGLJOBPROCESSOR_H__
#define OPENGLJOBPROCESSOR_H__

56
57
58
59
60
#include "cgt/glcontextmanager.h"
#include "cgt/job.h"
#include "cgt/runnable.h"
#include "cgt/singleton.h"
#include "cgt/singleton.h"
61

62
#include <ext/threading.h>
63

64
#define TBB_PREVIEW_MEMORY_POOL 1
65
66
67
68
#include <tbb/atomic.h>
#include <tbb/concurrent_queue.h>
#include <tbb/concurrent_hash_map.h>
#include <tbb/concurrent_vector.h>
69
#include <tbb/memory_pool.h>
70

71

72
namespace cgt {
73
74
75
    class GLCanvas;

    /**
76
     * This job processor singleton can be used to execute jobs that need an OpenGL context.
schultezub's avatar
schultezub committed
77
     * 
78
79
80
     * Implementing the Runnable interface, the OpenGLJobProcessor runs a background thread having
     * and acquired OpenGL context. You can execute OpenGL calls asynchroniously using enqueueJob()
     * or synchronously using the ScopedSynchronousGlJobExecution guard.
81
     */
82
83
    class CGT_API OpenGLJobProcessor : public cgt::Singleton<OpenGLJobProcessor>, public cgt::Runnable {
        friend class cgt::Singleton<OpenGLJobProcessor>;    ///< CRTP
84
        friend class AbstractJob;                           ///< so the custom new/delete operator can access the memory pool
85
86

    public:
87
88
89
90
91
92
        /**
         * Scope guard to ensure that encapsulated job is synchronously executed in an arbitrary OpenGL context.
         * This scope guard checks whether current thread is OpenGLJobProcessor thread. If so, it
         * does nothing. If this thread is not the OpenGL thread, the OpenGLJobProcessor is paused,
         * an arbitrary OpenGL context acquired. Upon destruction the OpenGLJobProcessor is resumed.
         */
93
        class CGT_API ScopedSynchronousGlJobExecution {
94
95
96
97
98
        public:
            ScopedSynchronousGlJobExecution();
            ~ScopedSynchronousGlJobExecution();

        private:
99
            cgt::GLContextScopedLock* _lock;
100
101
        };

102

schultezub's avatar
schultezub committed
103
104
105
        /**
         * Destructor, deletes all unfinished jobs.
         */
106
107
108
        virtual ~OpenGLJobProcessor();


schultezub's avatar
schultezub committed
109
110
111
112
        /**
         * Registers the given OpenGL context, so that it gets its own job queue.
         * \param   context     OpenGL context to register.
         */
113
        void setContext(cgt::GLCanvas* context);
114

115
116
117
118
        /**
         * Returns the OpenGL context of this object..
         * \return  _context
         */
119
        cgt::GLCanvas* getContext();
120

121
122
123
124
        /// \see Runnable::stop
        void stop();
        
        /**
schultezub's avatar
schultezub committed
125
         * Performs the job processing using conditional wait.
126
127
128
129
         * \sa Runnable::run
         */
        void run();

130
131
132
133
134
135
136
137
138
139
        /**
         * Pauses the job processor as at the next possible moment.
         */
        void pause();

        /**
         * Resume the execution of the job processor.
         */
        void resume();

140
        /**
141
         * Enqueues the given job.
142
143
144
145
         * 
         * \note    OpenGLJobProcessor takes ownership of \a job.
         * \param   job         Job to enqueue, PriorityPool takes ownership of this Job!
         */
146
        void enqueueJob(AbstractJob* job);
147

148
149
150
151
152
        /**
         * Enqueue OpenGL Garbage collection job
         */
        void enqueueGarbageCollection();

153
154

    protected:
155
156
157
158
159
        /**
         * Performs a OpenGL Garbage Collection if necessary.
         */
        void performGarbageCollectionIfNecessary();

160
        // Protected constructor since it's a singleton
161
162
        OpenGLJobProcessor();

163
        cgt::GLCanvas* _context;                        ///< The OpenGL context to use
164
        tbb::concurrent_queue<AbstractJob*> _jobQueue;  ///< The OpenGL job queue
165
        tbb::atomic<bool> _performGarbageCollection;    ///< Flag whether to perform garbage cxollection
166

167
        tbb::atomic<int> _pause;                        ///< Counter of pause requests
168
        std::condition_variable _evaluationCondition;   ///< conditional wait to be used when there are currently no jobs to process
169

170
    private:
171
        typedef std::allocator<AbstractJob> pool_allocator_t;
172
        tbb::memory_pool<pool_allocator_t> _jobPool;    ///< Memory pool for the signals
173
174
175
176
    };

}

177
#define GLJobProc cgt::Singleton<cgt::OpenGLJobProcessor>::getRef()
178
179

#endif // OPENGLJOBPROCESSOR_H__