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.43 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13
14
15
16
// 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
17
// 
18
19
20
21
// 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.
22
23
24
25
26
27
28
// 
// ================================================================================================

#ifndef OPENGLJOBPROCESSOR_H__
#define OPENGLJOBPROCESSOR_H__

#include "sigslot/sigslot.h"
29
30
31

#include "tgt/glcontextmanager.h"
#include "tgt/job.h"
32
33
#include "tgt/runnable.h"
#include "tgt/singleton.h"
34
#include "tgt/singleton.h"
35

36
#include <ext/threading.h>
37

38
#define TBB_PREVIEW_MEMORY_POOL 1
39
40
41
42
#include <tbb/atomic.h>
#include <tbb/concurrent_queue.h>
#include <tbb/concurrent_hash_map.h>
#include <tbb/concurrent_vector.h>
43
#include <tbb/memory_pool.h>
44

45

46
47
48
49
namespace tgt {
    class GLCanvas;

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

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

        private:
            tgt::GLContextScopedLock* _lock;
        };

76

schultezub's avatar
schultezub committed
77
78
79
        /**
         * Destructor, deletes all unfinished jobs.
         */
80
81
82
        virtual ~OpenGLJobProcessor();


schultezub's avatar
schultezub committed
83
84
85
86
        /**
         * Registers the given OpenGL context, so that it gets its own job queue.
         * \param   context     OpenGL context to register.
         */
87
        void setContext(tgt::GLCanvas* context);
88

89
90
91
92
93
94
        /**
         * Returns the OpenGL context of this object..
         * \return  _context
         */
        tgt::GLCanvas* getContext();

95
96
97
98
        /// \see Runnable::stop
        void stop();
        
        /**
schultezub's avatar
schultezub committed
99
         * Performs the job processing using conditional wait.
100
101
102
103
         * \sa Runnable::run
         */
        void run();

104
105
106
107
108
109
110
111
112
113
        /**
         * Pauses the job processor as at the next possible moment.
         */
        void pause();

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

114
        /**
115
         * Enqueues the given job.
116
117
118
119
         * 
         * \note    OpenGLJobProcessor takes ownership of \a job.
         * \param   job         Job to enqueue, PriorityPool takes ownership of this Job!
         */
120
        void enqueueJob(AbstractJob* job);
121

122
123
124
125
126
        /**
         * Enqueue OpenGL Garbage collection job
         */
        void enqueueGarbageCollection();

127
128

    protected:
129
130
131
132
133
        /**
         * Performs a OpenGL Garbage Collection if necessary.
         */
        void performGarbageCollectionIfNecessary();

134
        // Protected constructor since it's a singleton
135
136
        OpenGLJobProcessor();

137
138
        tgt::GLCanvas* _context;                        ///< The OpenGL context to use
        tbb::concurrent_queue<AbstractJob*> _jobQueue;  ///< The OpenGL job queue
139
        tbb::atomic<bool> _performGarbageCollection;    ///< Flag whether to perform garbage cxollection
140

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

144
    private:
145
        typedef std::allocator<AbstractJob> pool_allocator_t;
146
        tbb::memory_pool<pool_allocator_t> _jobPool;    ///< Memory pool for the signals
147
148
149
150
    };

}

151
#define GLJobProc tgt::Singleton<tgt::OpenGLJobProcessor>::getRef()
152
153

#endif // OPENGLJOBPROCESSOR_H__