abstractpipeline.h 9.07 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
#ifndef ABSTRACTPIPELINE_H__
#define ABSTRACTPIPELINE_H__

28
#include "sigslot/sigslot.h"
29

30
#include "tgt/logmanager.h"
31
#include "tgt/runnable.h"
32 33 34 35
#include "tgt/vector.h"
#include "tgt/event/eventhandler.h"
#include "tgt/event/eventlistener.h"

36 37
#include <tbb/spin_mutex.h>
#include <tbb/mutex.h>
38

39
#include "core/coreapi.h"
40
#include "core/datastructures/datacontainer.h"
41 42
#include "core/properties/datanameproperty.h"
#include "core/properties/floatingpointproperty.h"
schultezub's avatar
schultezub committed
43
#include "core/properties/propertycollection.h"
44

45
#include <map>
46 47
#include <vector>

48 49 50 51
namespace tgt {
    class GLCanvas;
}

schultezub's avatar
schultezub committed
52
namespace campvis {
schultezub's avatar
schultezub committed
53 54
    class AbstractProcessor;

55
    /**
schultezub's avatar
schultezub committed
56
     * Abstract base class for CAMPVis Pipelines.
57
     */
58
    class CAMPVIS_CORE_API AbstractPipeline : public HasPropertyCollection, public tgt::Runnable, public tgt::EventHandler, public tgt::EventListener {
59 60 61
    public:
        /**
         * Creates a AbstractPipeline.
62 63
         * If you derive from AbstractPipeline, you will have to implement the pipeline evaluation
         * logic yourself. You might want to have a look at AutoEvaluationPipeline.
64 65 66
         * 
         * \param   dc  Pointer to the DataContainer containing local working set of data for this 
         *              pipeline, must not be 0, must be valid the whole lifetime of this pipeline.
67
         */
68
        AbstractPipeline(DataContainer* dc);
69 70 71 72 73

        /**
         * Virtual Destructor
         **/
        virtual ~AbstractPipeline();
74 75 76 77 78 79
        
        /**
         * Gets the name of this very pipeline. To be defined by every subclass.
         * \return  The name of this pipeline.
         */
        virtual const std::string getName() const = 0;
80 81 82


        /**
83 84 85
         * Initializes this pipeline, its OpenGL context  and all of its processors.
         * Everything that requires a valid OpenGL context, valid construction of all other 
         * processors/properties or is otherwise expensive gets in here.
86
         * 
87
         * \note    Must be called with a valid and locked OpenGL context.
88
         * \note    When overwriting this method, make sure to call the base class version first.
89
         */
90
        virtual void init();
91

92
        /**
93 94 95 96 97
         * Deinitializes this pipeline, its OpenGL context and all of its processors.
         * Everything that requires a valid OpenGL context, cannont be executed during destruction
         * or is otherwise expensive gets in here.
         * 
         * \note    Must be called with a valid and locked OpenGL context.
98 99 100
         * \note    When overwriting this method, make sure to call the base class version first.
         */
        virtual void deinit();
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        
        /**
         * Adds the processor \a processor to this pipeline's processor list.
         * \note    The s_invalidated signal of each processor on this list will be automatically 
         *          connected to onProcessorInvalidated() during initialization and disconnected
         *          during deinitialization.
         * \param   processor   The processor to add.
         */
        virtual void addProcessor(AbstractProcessor* processor);
                
        /**
         * Performs the event handling for the assigned canvas.
         * Default behaviour is to execute all assigned EventHandlers, may be overwritten by subclasses.
         * \param e     event parameters
         */
        virtual void onEvent(tgt::Event* e);

118 119 120 121 122 123 124 125
        /**
         * Entrance point for the pipeline thread.
         * \see Runnable::run
         */
        virtual void run();

        /// \see Runnable::stop
        virtual void stop();
126

schultezub's avatar
schultezub committed
127
        /**
128
         * Returns the DataContainer of this pipeline, const version.
schultezub's avatar
schultezub committed
129 130 131
         * \return _data
         */
        const DataContainer& getDataContainer() const;
132

133 134 135 136 137 138
        /**
         * Returns the DataContainer of this pipeline, non-const version.
         * \return _data
         */
        DataContainer& getDataContainer();

139 140 141 142 143 144
        /**
         * Returns the list of processors of this pipeline.
         * \return  _processors
         */
        const std::vector<AbstractProcessor*>& getProcessors() const;

145 146 147 148 149 150 151 152 153 154 155 156
        /**
         * Gets the flag whether this pipeline is currently enabled.
         * \return _enabled
         */
        bool getEnabled() const;

        /**
         * Sets the flag whether this pipeline is currently enabled.
         * \param   enabled     New flag whether this pipeline is currently enabled.
         */
        void setEnabled(bool enabled);

157
        /**
158 159
         * Sets the Canvas hosting the OpenGL context for this pipeline.
         * \param   canvas  Canvas hosting the OpenGL context for this pipeline
160
         */
161
        void setCanvas(tgt::GLCanvas* canvas);
162 163

        /**
164 165
         * Sets the size of the render target
         * \param size  New viewport dimensions
166
         */
167
        void setRenderTargetSize(const tgt::ivec2& size);
168

schultezub's avatar
schultezub committed
169
        /**
170 171
         * Returns the viewport size of the target canvas
         * \return _canvasSize
schultezub's avatar
schultezub committed
172
         */
173
        const tgt::ivec2& getRenderTargetSize() const;
schultezub's avatar
schultezub committed
174 175

        /**
176 177
         * Returns the ID of the render target image to be rendered to the canvas
         * \return  The DataHandle named _renderTargetID in the pipeline's DataContainer, 0 if no such handle exists.
schultezub's avatar
schultezub committed
178
         */
179 180 181
        const std::string& getRenderTargetID() const;

        /// Signal emitted when the pipeline's render target has changed
182
        sigslot::signal0 s_renderTargetChanged;
183 184

    protected:
185
        /**
186
         * Sets the resultDirty flag of this pipeline and starts its execution if necessary.
187
         */
188 189
        void setPipelineDirty();

190
        /**
191 192 193 194 195 196 197
         * Executes this pipeline.
         * To be implemented in the subclass.
         */
        virtual void executePipeline() = 0;

        /**
         * Executes the processor \a processor on the pipeline's data and locks its properties meanwhile.
198 199
         * \param   processor   Processor to execute.
         */
200
        void executeProcessor(AbstractProcessor* processor);
201 202 203 204 205 206 207 208 209 210 211 212 213 214
        
        /**
         * Executes \a processor and afterwards checks the OpenGL state to be valid.
         * \note    Only call from with a valid OpenGL context
         * \param   processor   Processor to execute
         */
        void executeProcessorAndCheckOpenGLState(AbstractProcessor* processor);

        /**
         * Slot getting called when one of the observed properties changed and notifies its observers.
         * The default behaviour is just to set the invalidation level to invalid.
         * \param   prop    Property that emitted the signal
         */
        virtual void onPropertyChanged(const AbstractProperty* prop);
215 216


217 218
        /// Pointer to the DataContainer containing local working set of data for this Pipeline, must not be 0.
        DataContainer* _data;
219

220
        std::vector<AbstractProcessor*> _processors;        ///< List of all processors of this pipeline
schultezub's avatar
schultezub committed
221

222 223 224 225 226
        tgt::GLCanvas* _canvas;                             ///< Canvas hosting the OpenGL context for this pipeline.
        IVec2Property _canvasSize;                          ///< original canvas size
        bool _ignoreCanvasSizeUpdate;

        DataNameProperty _renderTargetID;                   ///< ID of the render target image to be rendered to the canvas
227

228 229 230 231 232
    private:
        std::condition_variable _evaluationCondition;       ///< conditional wait to be used when the pipeline currently does not need to be executed.
        tbb::atomic<bool> _enabled;                         ///< flag whether this pipeline is currently enabled
        tbb::atomic<bool> _pipelineDirty;                   ///< Flag whether this pipeline is dirty and executePipeline() needs to be called.

233
        static const std::string loggerCat_;
234

235 236 237 238 239 240
    };

}


#endif // ABSTRACTPIPELINE_H__