opengljobprocessor.h 5.21 KB
Newer Older
1
2
/**********************************************************************
 *                                                                    *
3
 * cgt - CAMP Graphics Toolbox, Copyright (C) 2012-2015               *
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
 *     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:
schultezub's avatar
schultezub committed
64
65
66
        /**
         * Destructor, deletes all unfinished jobs.
         */
67
68
69
        virtual ~OpenGLJobProcessor();


schultezub's avatar
schultezub committed
70
71
72
73
        /**
         * Registers the given OpenGL context, so that it gets its own job queue.
         * \param   context     OpenGL context to register.
         */
74
        void setContext(cgt::GLCanvas* context);
75

76
77
78
79
        /**
         * Returns the OpenGL context of this object..
         * \return  _context
         */
80
        cgt::GLCanvas* getContext();
81
82
        
        /**
schultezub's avatar
schultezub committed
83
         * Performs the job processing using conditional wait.
84
85
86
87
         * \sa Runnable::run
         */
        void run();

88
89
90
91
92
93
94
95
96
97
        /**
         * Pauses the job processor as at the next possible moment.
         */
        void pause();

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

98
        /**
99
         * Enqueues the given job.
100
101
102
103
         * 
         * \note    OpenGLJobProcessor takes ownership of \a job.
         * \param   job         Job to enqueue, PriorityPool takes ownership of this Job!
         */
104
        void enqueueJob(AbstractJob* job);
105

106
107
108
109
110
111
112
113
114
        /**
         * Enqueues the given job and blockes the execution until the job has been processed.
         * 
         * \note    OpenGLJobProcessor takes ownership of \a job.
         * \param   fn         Functor defining job to execute!
         */
        void enqueueJobBlocking(std::function<void(void)> fn);


115
    protected:
116
        // Protected constructor since it's a singleton
117
118
        OpenGLJobProcessor();

119
        cgt::GLCanvas* _context;                        ///< The OpenGL context to use
120
        tbb::concurrent_queue<AbstractJob*> _jobQueue;  ///< The OpenGL job queue
121

122
        tbb::atomic<int> _pause;                        ///< Counter of pause requests
123

124
    private:
125
        typedef std::allocator<AbstractJob> pool_allocator_t;
126
        tbb::memory_pool<pool_allocator_t> _jobPool;    ///< Memory pool for the signals
127
128
129
130
    };

}

131
#define GLJobProc cgt::Singleton<cgt::OpenGLJobProcessor>::getRef()
132
133

#endif // OPENGLJOBPROCESSOR_H__