1 #ifndef COIN_SONODEPROFILING_H 2 #define COIN_SONODEPROFILING_H 3 4 /**************************************************************************\ 5 * Copyright (c) Kongsberg Oil & Gas Technologies AS 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are 10 * met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * Neither the name of the copyright holder nor the names of its 20 * contributors may be used to endorse or promote products derived from 21 * this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 \**************************************************************************/ 35 36 #include <Inventor/actions/SoAction.h> 37 #include <Inventor/actions/SoGLRenderAction.h> 38 #include <Inventor/nodes/SoGroup.h> 39 #include <Inventor/system/gl.h> 40 #include <Inventor/annex/Profiler/SoProfiler.h> 41 #include <Inventor/annex/Profiler/elements/SoProfilerElement.h> 42 43 #include "misc/SoDBP.h" // for global envvar COIN_PROFILER 44 #include "profiler/SoProfilerP.h" 45 46 /* 47 The SoNodeProfiling class contains instrumentation code for scene 48 graph profiling. 49 50 The convention for using these preTraversal and postTraversal 51 methods is that the parent node only applies these around child 52 traversal invokations, and not around itself. This is really 53 important to get straight. As a consequence of this, the applying 54 action also needs to add preTraversal()/postTraversal() around 55 invoking traversal of the root. 56 57 If you combine doing both, then you get a lot of double-booking of 58 timings and negative timing offsets, which causes mayhem in the 59 statistics, and was a mess to figure out. 60 */ 61 62 class SoNodeProfiling { 63 public: SoNodeProfiling(void)64 SoNodeProfiling(void) 65 : pretime(SbTime::zero()), entryindex(-1) 66 { 67 } 68 preTraversal(SoAction * action)69 void preTraversal(SoAction * action) 70 { 71 if (!SoNodeProfiling::isActive(action)) return; 72 73 SoState * state = action->getState(); 74 SoProfilerElement * profilerelt = SoProfilerElement::get(state); 75 SbProfilingData & data = profilerelt->getProfilingData(); 76 const SoFullPath * fullpath = 77 static_cast<const SoFullPath *>(action->getCurPath()); 78 this->entryindex = data.getIndex(fullpath, TRUE); 79 assert(this->entryindex != -1); 80 size_t managedmem = 0, unmanagedmem = 0; 81 fullpath->getTail()->getFieldsMemorySize(managedmem, unmanagedmem); 82 data.setNodeFootprint(this->entryindex, 83 SbProfilingData::MEMORY_SIZE, managedmem); 84 data.setNodeFootprint(this->entryindex, 85 SbProfilingData::VIDEO_MEMORY_SIZE, 0); 86 this->pretime = SbTime::getTimeOfDay(); 87 } 88 postTraversal(SoAction * action)89 void postTraversal(SoAction * action) 90 { 91 if (!SoNodeProfiling::isActive(action)) return; 92 93 if (action->isOfType(SoGLRenderAction::getClassTypeId()) && 94 SoProfilerP::shouldSyncGL()) 95 glFinish(); 96 97 const SbTime duration(SbTime::getTimeOfDay() - this->pretime); 98 99 SoState * state = action->getState(); 100 SoProfilerElement * profilerelt = SoProfilerElement::get(state); 101 SbProfilingData & data = profilerelt->getProfilingData(); 102 103 assert(this->entryindex != -1); 104 int parentindex = data.getParentIndex(this->entryindex); 105 if (parentindex != -1) { 106 data.preOffsetNodeTiming(parentindex, -duration); 107 } 108 109 // see if a children offset has been stored for us and just add timing 110 // duration data to that 111 const SbTime childrenoffset(data.getNodeTiming(this->entryindex)); 112 // childrenoffset will deduct the child node timings from this timing 113 const SbTime adjusted(childrenoffset + duration); 114 assert(adjusted.getValue() >= 0.0); 115 data.setNodeTiming(this->entryindex, adjusted); 116 #if 0 // DEBUG 117 const SoFullPath * fullpath = (const SoFullPath *)action->getCurPath(); 118 SoDebugError::postInfo("Profiling", 119 "%20s (%d): duration %g, offset %g, adjusted %g", 120 fullpath->getTail()->getTypeId().getName().getString(), 121 fullpath->getLength(), 122 duration.getValue(), childrenoffset.getValue(), 123 adjusted.getValue()); 124 #endif 125 } 126 isActive(SoAction * action)127 static bool isActive(SoAction * action) 128 { 129 if (!SoProfiler::isEnabled()) return false; 130 SoState * state = action->getState(); 131 return state->isElementEnabled(SoProfilerElement::getClassStackIndex()) ? true : false; 132 } 133 134 private: 135 SbTime pretime; 136 int entryindex; 137 138 }; 139 140 #endif // !COIN_SONODEPROFILING_H 141