abstractpipeline.cpp 10.5 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() {
67

schultezub's avatar
schultezub committed
68
69
        initAllProperties();

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

81
    void AbstractPipeline::deinit() {
schultezub's avatar
schultezub committed
82
83
        deinitAllProperties();

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

95

96
        // clear DataContainer
97
        _data->clear();
98
99
    }

100
    void AbstractPipeline::run() {
101
102
        std::unique_lock<std::mutex> lock(*cgt::GlContextManager::getRef().getGlMutexForContext(_canvas));
        cgt::GlContextManager::getRef().acquireContext(_canvas, false);
103
104
105
106
107
108
109
110
111
112
113
114
115

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

                // execute pipeline
                executePipeline();

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

116
            if (!_stopExecution && (!_enabled || !_pipelineDirty)) {
117
                cgt::GlContextManager::getRef().releaseContext(_canvas, false);
118
                _evaluationCondition.wait(lock);
119
                cgt::GlContextManager::getRef().acquireContext(_canvas, false);
120
121
122
            }
        }

123
        cgt::GlContextManager::getRef().releaseContext(_canvas, false);
124
125
126
127
128
129
130
131
132
    }

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

        Runnable::stop();
    }

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

    const DataContainer& AbstractPipeline::getDataContainer() const {
147
        return *_data;
schultezub's avatar
schultezub committed
148
149
    }

150
    DataContainer& AbstractPipeline::getDataContainer() {
151
        return *_data;
152
153
    }

154
    void AbstractPipeline::executeProcessor(AbstractProcessor* processor) {
155
        cgtAssert(processor != 0, "Processor must not be 0.");
156

157
        // execute processor if needed
158
        if (processor->getEnabled() && !processor->isLocked()) {
159
            if (! processor->isValid()) {
160
161
162
                tbb::tick_count startTime;
                if (processor->getClockExecutionTime())
                    startTime = tbb::tick_count::now();
163
164

                try {
165
                    processor->process(*_data);
166
167
168
169
170
171
172
173
174
                }
                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()) {
175
176
                    tbb::tick_count endTime = tbb::tick_count::now();
                    LINFO("Executed processor " << processor->getName() << " duration: " << (endTime - startTime).seconds());
177
                }
178
            }
179
        }
180
181
    }

182
183
184
185
    void AbstractPipeline::executeProcessorAndCheckOpenGLState(AbstractProcessor* processor) {
        AbstractPipeline::executeProcessor(processor);

#ifdef CAMPVIS_DEBUG
186
187
        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.");
188

189
190
        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.");
191

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

194
195
196
197
        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.");
198

199
200
201
202
        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.");
203
204
205
#endif
    }

206
207
208
209
    const std::vector<AbstractProcessor*>& AbstractPipeline::getProcessors() const {
        return _processors;
    }

210
211
212
213
214
215
    bool AbstractPipeline::getEnabled() const {
        return _enabled;
    }

    void AbstractPipeline::setEnabled(bool enabled) {
        _enabled = enabled;
216
        setPipelineDirty();
217
218
    }

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

            if (e->isAccepted())
                break;
233
234
235
        }
    }

236
    void AbstractPipeline::setCanvas(cgt::GLCanvas* canvas) {
237
238
239
        _canvas = canvas;
    }

240
    void AbstractPipeline::setRenderTargetSize(const cgt::ivec2& size) {
241
242
243
244
245
        if (_canvasSize.getValue() != size && !_ignoreCanvasSizeUpdate) {
            _canvasSize.setValue(size);
        }
    }

246
    const cgt::ivec2& AbstractPipeline::getRenderTargetSize() const {
247
248
249
250
251
252
253
        return _canvasSize.getValue();
    }

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

254
    void AbstractPipeline::addProcessor(AbstractProcessor* processor) {
255
        cgtAssert(processor != 0, "Processor must not be 0.")
256
257
        _processors.push_back(processor);
    }
258

259
260
    void AbstractPipeline::setPipelineDirty() {
        _pipelineDirty = true;
261

262
263
        if (_enabled)
            _evaluationCondition.notify_all();
264
    }
265
266
267
268
269
270
271
272
273
274
275
276

    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;
    }



277
}