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 5.78 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
31
32

#ifndef OPENGLJOBPROCESSOR_H__
#define OPENGLJOBPROCESSOR_H__

33
34
35
36
37
#include "cgt/glcontextmanager.h"
#include "cgt/job.h"
#include "cgt/runnable.h"
#include "cgt/singleton.h"
#include "cgt/singleton.h"
38

39
#include <ext/threading.h>
40

41
#define TBB_PREVIEW_MEMORY_POOL 1
42
43
44
45
#include <tbb/atomic.h>
#include <tbb/concurrent_queue.h>
#include <tbb/concurrent_hash_map.h>
#include <tbb/concurrent_vector.h>
46
#include <tbb/memory_pool.h>
47

48

49
namespace cgt {
50
51
52
    class GLCanvas;

    /**
53
     * This job processor singleton can be used to execute jobs that need an OpenGL context.
schultezub's avatar
schultezub committed
54
     * 
55
56
57
     * 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.
58
     */
59
    class CGT_API OpenGLJobProcessor : public cgt::Singleton<OpenGLJobProcessor>, public cgt::RunnableWithConditionalWait {
60
        friend class cgt::Singleton<OpenGLJobProcessor>;    ///< CRTP
61
        friend class AbstractJob;                           ///< so the custom new/delete operator can access the memory pool
62
63

    public:
64
65
66
67
68
69
        /**
         * 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.
         */
70
        class CGT_API ScopedSynchronousGlJobExecution {
71
72
73
74
75
        public:
            ScopedSynchronousGlJobExecution();
            ~ScopedSynchronousGlJobExecution();

        private:
76
77
78
79
            // disable copying
            explicit ScopedSynchronousGlJobExecution(const ScopedSynchronousGlJobExecution& rhs);
            ScopedSynchronousGlJobExecution& operator=(ScopedSynchronousGlJobExecution rhs);

80
            cgt::GLContextScopedLock* _lock;
81
82
        };

83

schultezub's avatar
schultezub committed
84
85
86
        /**
         * Destructor, deletes all unfinished jobs.
         */
87
88
89
        virtual ~OpenGLJobProcessor();


schultezub's avatar
schultezub committed
90
91
92
93
        /**
         * Registers the given OpenGL context, so that it gets its own job queue.
         * \param   context     OpenGL context to register.
         */
94
        void setContext(cgt::GLCanvas* context);
95

96
97
98
99
        /**
         * Returns the OpenGL context of this object..
         * \return  _context
         */
100
        cgt::GLCanvas* getContext();
101
102
        
        /**
schultezub's avatar
schultezub committed
103
         * Performs the job processing using conditional wait.
104
105
106
107
         * \sa Runnable::run
         */
        void run();

108
109
110
111
112
113
114
115
116
117
        /**
         * Pauses the job processor as at the next possible moment.
         */
        void pause();

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

118
        /**
119
         * Enqueues the given job.
120
121
122
123
         * 
         * \note    OpenGLJobProcessor takes ownership of \a job.
         * \param   job         Job to enqueue, PriorityPool takes ownership of this Job!
         */
124
        void enqueueJob(AbstractJob* job);
125
126

    protected:
127
        // Protected constructor since it's a singleton
128
129
        OpenGLJobProcessor();

130
        cgt::GLCanvas* _context;                        ///< The OpenGL context to use
131
        tbb::concurrent_queue<AbstractJob*> _jobQueue;  ///< The OpenGL job queue
132

133
        tbb::atomic<int> _pause;                        ///< Counter of pause requests
134

135
    private:
136
        typedef std::allocator<AbstractJob> pool_allocator_t;
137
        tbb::memory_pool<pool_allocator_t> _jobPool;    ///< Memory pool for the signals
138
139
140
141
    };

}

142
#define GLJobProc cgt::Singleton<cgt::OpenGLJobProcessor>::getRef()
143
144

#endif // OPENGLJOBPROCESSOR_H__