1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif // HAVE_CONFIG_H
36 
37 #ifdef HAVE_NODEKITS
38 
39 #include <Inventor/annex/Profiler/nodekits/SoProfilerVisualizeKit.h>
40 #include "coindefs.h"
41 
42 #include <boost/scoped_ptr.hpp>
43 
44 #include <Inventor/actions/SoGLRenderAction.h>
45 #include <Inventor/annex/Profiler/nodekits/SoNodeVisualize.h>
46 #include <Inventor/annex/Profiler/nodes/SoProfilerStats.h>
47 #include <Inventor/elements/SoCacheElement.h>
48 #include <Inventor/nodes/SoBaseColor.h>
49 #include <Inventor/nodes/SoDirectionalLight.h>
50 #include <Inventor/nodes/SoLightModel.h>
51 #include <Inventor/nodes/SoMaterial.h>
52 #include <Inventor/nodes/SoScale.h>
53 #include <Inventor/nodes/SoSeparator.h>
54 #include <Inventor/nodes/SoTranslation.h>
55 #include <Inventor/sensors/SoFieldSensor.h>
56 
57 #include "nodekits/SoSubKitP.h"
58 
59 namespace {
60   // anonymous namespace for trigger functions, to change how
61   // visualization occur.
cacheSensorCB(void * data,SoSensor * COIN_UNUSED_ARG (sense))62   void cacheSensorCB(void * data, SoSensor * COIN_UNUSED_ARG(sense)){
63     SoProfilerVisualizeKit * kit = (SoProfilerVisualizeKit*)data;
64     /*SoNode * root = */kit->root.getValue();
65     if(!kit->root.getValue())
66       return;
67 
68     SoNodeVisualize * nv = (SoNodeVisualize*)kit->getPart("visualtree", FALSE);
69     if(nv){
70       SoProfilerStats * stats = (SoProfilerStats*)kit->stats.getValue();
71       assert(stats->getTypeId() == SoProfilerStats::getClassTypeId() &&
72              "Wake up and get your act together, will you?");
73 
74       nv->traverse(stats);
75     }
76   }
77 
rootChangedCB(void * data,SoSensor * COIN_UNUSED_ARG (sense))78   void rootChangedCB(void * data, SoSensor * COIN_UNUSED_ARG(sense)){
79     // FIXME 20071109 rolvs: Is it possible to automatically detect structural changes
80     // in the scenegraph? Perhaps from Inventor/misc/SoNotRec.h or something. Now
81     // we build the SoNodeVisualize-tree on every root-change
82     SoProfilerVisualizeKit * kit = (SoProfilerVisualizeKit*)data;
83     if(!kit->getPart("visualtree", FALSE))
84       if(kit->root.getValue())
85         kit->setPart("visualtree", SoNodeVisualize::visualizeTree(kit->root.getValue(), 1));
86   }
87 
statsCB(void * data,SoSensor * COIN_UNUSED_ARG (s))88   void statsCB(void * data, SoSensor * COIN_UNUSED_ARG(s)) {
89     SoProfilerVisualizeKit * kit = (SoProfilerVisualizeKit*)data;
90     kit->statsTrigger.disconnect();
91     SoNode * node = kit->stats.getValue();
92     if (node != NULL) {
93       if (node->isOfType(SoProfilerStats::getClassTypeId())) {
94         SoProfilerStats * statsnode = (SoProfilerStats *)node;
95         kit->statsTrigger.connectFrom(&statsnode->profilingUpdate);
96       } else {
97 #if COIN_DEBUG
98         static SbBool first = TRUE;
99         if (first) {
100           SoDebugError::postWarning("SoProfilerVisualizeKit.cpp rootChangedCB",
101                                     "The node i SoProfilerVisualizeKit::stats"
102                                     "must be of type SoProfilerStats.",
103                                     kit->getTypeId().getName().getString());
104           first = FALSE;
105         }
106 #endif // COIN_DEBUG
107       }
108     }
109   }
110 
statsTriggerCB(void * data,SoSensor * s)111   void statsTriggerCB(void * data, SoSensor * s) {
112     SoProfilerVisualizeKit * kit = (SoProfilerVisualizeKit*)data;
113     if (kit->stats.getValue() != NULL)
114       cacheSensorCB(data, s);
115   }
116 };
117 
118 struct SoProfilerVisualizeKitP
119 {
120   // Sensors
121   boost::scoped_ptr<SoFieldSensor> cacheSensor;
122   boost::scoped_ptr<SoFieldSensor> rootSensor;
123   boost::scoped_ptr<SoFieldSensor> statsTriggerSensor;
124   boost::scoped_ptr<SoFieldSensor> statsSensor;
125 };
126 
127 #define PRIVATE(x) ((x)->pimpl)
128 
129 SO_KIT_SOURCE(SoProfilerVisualizeKit);
130 
131 
132 // Docs from parent
initClass()133 void SoProfilerVisualizeKit::initClass()
134 {
135   SO_KIT_INTERNAL_INIT_CLASS(SoProfilerVisualizeKit, SO_FROM_COIN_3_0);
136 }
137 
138 
SoProfilerVisualizeKit()139 SoProfilerVisualizeKit::SoProfilerVisualizeKit()
140 {
141   SO_KIT_INTERNAL_CONSTRUCTOR(SoProfilerVisualizeKit);
142   SO_KIT_ADD_CATALOG_ENTRY(top, SoSeparator, FALSE, this, "", TRUE);
143   SO_KIT_ADD_CATALOG_ENTRY(pretree, SoGroup, FALSE, top, visualtree, TRUE);
144   SO_KIT_ADD_CATALOG_ENTRY(visualtree, SoNodeVisualize, TRUE, top, "", TRUE);
145   SO_KIT_INIT_INSTANCE();
146 
147   SO_KIT_ADD_FIELD(stats, (NULL));
148   SO_KIT_ADD_FIELD(statsTrigger, ());
149   SO_KIT_ADD_FIELD(root, (NULL));
150   SO_KIT_ADD_FIELD(separatorsWithGLCaches, (NULL));
151 
152   this->separatorsWithGLCaches.setNum(0);
153   this->separatorsWithGLCaches.setDefault(FALSE);
154   PRIVATE(this)->cacheSensor.reset(new SoFieldSensor(cacheSensorCB, this));
155   PRIVATE(this)->cacheSensor->attach(&this->separatorsWithGLCaches);
156   PRIVATE(this)->rootSensor.reset(new SoFieldSensor(rootChangedCB, this));
157   PRIVATE(this)->rootSensor->attach(&this->root);
158   PRIVATE(this)->statsTriggerSensor.reset(new SoFieldSensor(statsTriggerCB, this));
159   PRIVATE(this)->statsTriggerSensor->attach(&this->statsTrigger);
160   PRIVATE(this)->statsSensor.reset(new SoFieldSensor(statsCB, this));
161   PRIVATE(this)->statsSensor->attach(&this->stats);
162 
163   SoGroup * pretree = (SoGroup *)this->getAnyPart("pretree", TRUE);
164 
165   SoScale * treescale = new SoScale;
166   treescale->scaleFactor.setValue(0.02f, 0.02f, 1.0f);
167   SoLightModel * lm = new SoLightModel;
168   lm->model.setValue(SoLightModel::BASE_COLOR);
169   SoBaseColor * bc = new SoBaseColor;
170   bc->rgb.setValue(1.0, 1.0, 1.0);
171   SoDirectionalLight * dl = new SoDirectionalLight;
172   SoMaterial * m = new SoMaterial;
173   SoTranslation * tr = new SoTranslation;
174   tr->translation.setValue(0.0f, 0.0f, -2.0f);
175   pretree->addChild(treescale);
176   pretree->addChild(lm);
177   pretree->addChild(dl);
178   pretree->addChild(m);
179   pretree->addChild(tr);
180 }
181 
~SoProfilerVisualizeKit()182 SoProfilerVisualizeKit::~SoProfilerVisualizeKit()
183 {
184 }
185 
186 /*
187 void
188 SoProfilerVisualizeKit::GLRender(SoGLRenderAcion * action)
189 {
190   SoState * state = action->getState();
191   if (state->isElementEnabled(SoCacheElement::getClassStackIndex())) {
192     SoCacheElement::invalidate(state);
193   }
194 
195 }
196 */
197 
198 #undef PRIVATE
199 
200 #endif // HAVE_NODEKITS
201