abstractpipeline.cpp 10.7 KB
Newer Older
1

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

26
#include "abstractpipeline.h"
schultezub's avatar
schultezub committed
27

28
29
#include <tbb/tick_count.h>

30
31
32
33
#include "cgt/exception.h"
#include "cgt/glcanvas.h"
#include "cgt/glcontextmanager.h"
#include "cgt/painter.h"
34
#include "cgt/cgt_gl.h"
35
36

#include "core/pipeline/visualizationprocessor.h"
37
#include "core/pipeline/abstractprocessor.h"
38

39

schultezub's avatar
schultezub committed
40
41
namespace campvis {
    const std::string AbstractPipeline::loggerCat_ = "CAMPVis.core.datastructures.AbstractPipeline";
42

43
    AbstractPipeline::AbstractPipeline(DataContainer* dc) 
44
        : HasPropertyCollection()
45
46
        , cgt::EventHandler()
        , cgt::EventListener()
47
        , _data(dc)
48
        , _canvas(0)
49
        , _canvasSize("CanvasSize", "Canvas Size", cgt::ivec2(128, 128), cgt::ivec2(1, 1), cgt::ivec2(4096, 4096))
50
51
        , _ignoreCanvasSizeUpdate(false)
        , _renderTargetID("renderTargetID", "Render Target ID", "AbstractPipeline.renderTarget", DataNameProperty::READ)
schultezub's avatar
   
schultezub committed
52
    {
53
        cgtAssert(_data != 0, "Pointer to the DataContainer for this pipeline must not be 0!");
54

55
        _enabled = false;
56
        _pipelineDirty = true;
57

58
59
        addProperty(_renderTargetID);
        addProperty(_canvasSize);
schultezub's avatar
   
schultezub committed
60
61
62
    }

    AbstractPipeline::~AbstractPipeline() {
63

schultezub's avatar
   
schultezub committed
64
65
    }

66
    void AbstractPipeline::init() {
schultezub's avatar
schultezub committed
67
68
        initAllProperties();

69
        // initialize all processors:
schultezub's avatar
schultezub committed
70
71
72
73
        for (std::vector<AbstractProcessor*>::iterator it = _processors.begin(); it != _processors.end(); ++it) {
            try {
                (*it)->init();
            }
74
            catch (cgt::Exception& e) {
schultezub's avatar
schultezub committed
75
76
77
                LERROR("Caught Exception during initialization of processor: " << e.what());
            }
        }
78
79
80

        // use trigger signal to enforce blocking call
        s_init.triggerSignal();
81
    }
schultezub's avatar
schultezub committed
82

83
    void AbstractPipeline::deinit() {
84
85
86
        // use trigger signal to enforce blocking call
        s_deinit.triggerSignal();

schultezub's avatar
schultezub committed
87
88
        deinitAllProperties();

89
90
91
        // deinitialize all processors:
        for (std::vector<AbstractProcessor*>::iterator it = _processors.begin(); it != _processors.end(); ++it) {
            try {
schultezub's avatar
schultezub committed
92
                (*it)->s_invalidated.disconnect(this);
93
94
                (*it)->deinit();
            }
95
            catch (cgt::Exception& e) {
96
97
98
                LERROR("Caught Exception during deinitialization of processor: " << e.what());
            }
        }
99

100

101
        // clear DataContainer
102
        _data->clear();
103
104
    }

105
    void AbstractPipeline::run() {
106
107
        std::unique_lock<std::mutex> lock(*cgt::GlContextManager::getRef().getGlMutexForContext(_canvas));
        cgt::GlContextManager::getRef().acquireContext(_canvas, false);
108
109
110
111
112
113
114
115
116
117
118
119
120

        while (! _stopExecution) {
            if (_enabled && _pipelineDirty) {
                // mark pipeline as not dirty
                _pipelineDirty = false;

                // execute pipeline
                executePipeline();

                // paint on canvas
                _canvas->getPainter()->paint();
            }

121
            if (!_stopExecution && (!_enabled || !_pipelineDirty)) {
122
                cgt::GlContextManager::getRef().releaseContext(_canvas, false);
123
                _evaluationCondition.wait(lock);
124
                cgt::GlContextManager::getRef().acquireContext(_canvas, false);
125
126
127
            }
        }

128
        cgt::GlContextManager::getRef().releaseContext(_canvas, false);
129
130
131
132
133
134
135
136
137
    }

    void AbstractPipeline::stop() {
        _stopExecution = true;
        _evaluationCondition.notify_all();

        Runnable::stop();
    }

schultezub's avatar
schultezub committed
138
    void AbstractPipeline::onPropertyChanged(const AbstractProperty* prop) {
139
        if (prop == &_renderTargetID) {
140
            setPipelineDirty();
141
        }
142
        else if (prop == &_canvasSize && _canvas != nullptr && !_ignoreCanvasSizeUpdate) {
143
144
145
146
147
148
            if (_canvasSize.getValue() != _canvas->getSize()) {
                _ignoreCanvasSizeUpdate = true;
                _canvas->setSize(_canvasSize.getValue());
                _ignoreCanvasSizeUpdate = false;
            }
        }
schultezub's avatar
schultezub committed
149
150
151
    }

    const DataContainer& AbstractPipeline::getDataContainer() const {
152
        return *_data;
schultezub's avatar
schultezub committed
153
154
    }

155
    DataContainer& AbstractPipeline::getDataContainer() {
156
        return *_data;
157
158
    }

159
    void AbstractPipeline::executeProcessor(AbstractProcessor* processor) {
160
        cgtAssert(processor != 0, "Processor must not be 0.");
161

162
        // execute processor if needed
163
        if (processor->getEnabled() && !processor->isLocked()) {
164
            if (! processor->isValid()) {
165
166
167
                tbb::tick_count startTime;
                if (processor->getClockExecutionTime())
                    startTime = tbb::tick_count::now();
168
169

                try {
170
                    processor->process(*_data);
171
172
173
174
175
176
177
178
179
                }
                catch (std::exception& e) {
                    LERROR("Caught unhandled exception while executing processor " << processor->getName() << ": " << e.what());
                }
                catch (...) {
                    LERROR("Caught unhandled exception while executing processor " << processor->getName() << ": unknown exception");
                }

                if (processor->getClockExecutionTime()) {
180
181
                    tbb::tick_count endTime = tbb::tick_count::now();
                    LINFO("Executed processor " << processor->getName() << " duration: " << (endTime - startTime).seconds());
182
                }
183
            }
184
        }
185
186
    }

187
188
189
190
    void AbstractPipeline::executeProcessorAndCheckOpenGLState(AbstractProcessor* processor) {
        AbstractPipeline::executeProcessor(processor);

#ifdef CAMPVIS_DEBUG
191
192
        cgtAssert(cgt::getGlBool(GL_DEPTH_TEST) == false, "Invalid OpenGL state after processor execution, GL_DEPTH_TEST != false.");
        cgtAssert(cgt::getGlBool(GL_SCISSOR_TEST) == false, "Invalid OpenGL state after processor execution, GL_SCISSOR_TEST != false.");
193

194
195
        cgtAssert(cgt::getGlInt(GL_CULL_FACE_MODE) == GL_BACK, "Invalid OpenGL state after processor execution, GL_CULL_FACE_MODE != GL_BACk.");
        cgtAssert(cgt::getGlInt(GL_DEPTH_FUNC) == GL_LESS, "Invalid OpenGL state after processor execution, GL_DEPTH_FUNC != GL_LESS.");
196

197
        cgtAssert(cgt::getGlFloat(GL_DEPTH_CLEAR_VALUE) == 1.f, "Invalid OpenGL state after processor execution, GL_DEPTH_CLEAR_VALUE != 1.f.");
198

199
200
201
202
        cgtAssert(cgt::getGlFloat(GL_RED_SCALE) == 1.f, "Invalid OpenGL state after processor execution, GL_RED_SCALE != 1.f.");
        cgtAssert(cgt::getGlFloat(GL_GREEN_SCALE) == 1.f, "Invalid OpenGL state after processor execution, GL_GREEN_SCALE != 1.f.");
        cgtAssert(cgt::getGlFloat(GL_BLUE_SCALE) == 1.f, "Invalid OpenGL state after processor execution, GL_BLUE_SCALE != 1.f.");
        cgtAssert(cgt::getGlFloat(GL_ALPHA_SCALE) == 1.f, "Invalid OpenGL state after processor execution, GL_ALPHA_SCALE != 1.f.");
203

204
205
206
207
        cgtAssert(cgt::getGlFloat(GL_RED_BIAS) == 0.f, "Invalid OpenGL state after processor execution, GL_RED_BIAS != 0.f.");
        cgtAssert(cgt::getGlFloat(GL_GREEN_BIAS) == 0.f, "Invalid OpenGL state after processor execution, GL_GREEN_BIAS != 0.f.");
        cgtAssert(cgt::getGlFloat(GL_BLUE_BIAS) == 0.f, "Invalid OpenGL state after processor execution, GL_BLUE_BIAS != 0.f.");
        cgtAssert(cgt::getGlFloat(GL_ALPHA_BIAS) == 0.f, "Invalid OpenGL state after processor execution, GL_ALPHA_BIAS != 0.f.");
208
209
210
#endif
    }

211
212
213
214
    const std::vector<AbstractProcessor*>& AbstractPipeline::getProcessors() const {
        return _processors;
    }

215
216
217
218
219
220
    bool AbstractPipeline::getEnabled() const {
        return _enabled;
    }

    void AbstractPipeline::setEnabled(bool enabled) {
        _enabled = enabled;
221
        setPipelineDirty();
222
223
    }

224
225
    void AbstractPipeline::onEvent(cgt::Event* e) {
        // copy and paste from cgt::EventHandler::onEvent() but without deleting e
226
        for (size_t i = 0 ; i < listeners_.size() ; ++i) {
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
227
228
            // don't forward this event to ourselves - otherwise we'll end up with an endless loop.
            if (listeners_[i] == this) {
229
                cgt::EventListener::onEvent(e);
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
230
            }
231
            // check if current listener listens to the eventType of e
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
232
            else if (listeners_[i]->getEventTypes() & e->getEventType() ){
233
234
                listeners_[i]->onEvent(e);
            }
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
235
236
237

            if (e->isAccepted())
                break;
238
239
240
        }
    }

241
    void AbstractPipeline::setCanvas(cgt::GLCanvas* canvas) {
242
243
244
        _canvas = canvas;
    }

245
    void AbstractPipeline::setRenderTargetSize(const cgt::ivec2& size) {
246
247
248
249
250
        if (_canvasSize.getValue() != size && !_ignoreCanvasSizeUpdate) {
            _canvasSize.setValue(size);
        }
    }

251
    const cgt::ivec2& AbstractPipeline::getRenderTargetSize() const {
252
253
254
255
256
257
258
        return _canvasSize.getValue();
    }

    const std::string& AbstractPipeline::getRenderTargetID() const {
        return _renderTargetID.getValue();
    }

259
    void AbstractPipeline::addProcessor(AbstractProcessor* processor) {
260
        cgtAssert(processor != 0, "Processor must not be 0.")
261
262
        _processors.push_back(processor);
    }
263

264
265
    void AbstractPipeline::setPipelineDirty() {
        _pipelineDirty = true;
266

267
268
        if (_enabled)
            _evaluationCondition.notify_all();
269
    }
270
271
272
273
274
275
276
277
278
279
280
281

    AbstractProcessor* AbstractPipeline::getProcessor(const std::string& name) const {
        for (auto it = _processors.begin(); it != _processors.end(); ++it) {
            if ((*it)->getName() == name)
                return *it;
        }

        return nullptr;
    }



282
}