| 1 | // fg_os_osgviewer.cxx -- common functions for fg_os interface |
| 2 | // implemented as an osgViewer |
| 3 | // |
| 4 | // Copyright (C) 2007 Tim Moore [email protected] |
| 5 | // Copyright (C) 2007 Mathias Froehlich |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | |
| 21 | #ifdef __linux__ |
| 22 | #include <sched.h> |
| 23 | #endif |
| 24 | |
| 25 | #include <config.h> |
| 26 | |
| 27 | #include <algorithm> |
| 28 | #include <cstdlib> |
| 29 | #include <iostream> |
| 30 | #include <sstream> |
| 31 | #include <string> |
| 32 | |
| 33 | #include <simgear/compiler.h> |
| 34 | #include <simgear/debug/logstream.hxx> |
| 35 | #include <simgear/props/props_io.hxx> |
| 36 | #include <simgear/structure/exception.hxx> |
| 37 | #include <simgear/scene/util/OsgIoCapture.hxx> |
| 38 | |
| 39 | #include <osg/Camera> |
| 40 | #include <osg/GraphicsContext> |
| 41 | #include <osg/Group> |
| 42 | #include <osg/Matrixd> |
| 43 | #include <osg/Notify> |
| 44 | #include <osg/Version> |
| 45 | #include <osg/View> |
| 46 | #include <osg/Viewport> |
| 47 | #include <osgViewer/GraphicsWindow> |
| 48 | #include <osgViewer/Viewer> |
| 49 | #include <osgViewer/ViewerEventHandlers> |
| 50 | |
| 51 | #include "CameraGroup.hxx" |
| 52 | #include "FGEventHandler.hxx" |
| 53 | #include "VRManager.hxx" |
| 54 | #include "WindowBuilder.hxx" |
| 55 | #include "WindowSystemAdapter.hxx" |
| 56 | #include "renderer.hxx" |
| 57 | #include <Main/fg_os.hxx> |
| 58 | #include <Main/fg_props.hxx> |
| 59 | #include <Main/globals.hxx> |
| 60 | #include <Main/sentryIntegration.hxx> |
| 61 | #include <Main/util.hxx> |
| 62 | #include <Scenery/scenery.hxx> |
| 63 | |
| 64 | #if defined(SG_MAC) |
| 65 | #include <GUI/CocoaHelpers.h> |
| 66 | #endif |
| 67 | |
| 68 | #if defined(SG_WINDOWS) |
| 69 | #include <process.h> // _getpid() |
| 70 | #endif |
| 71 | |
| 72 | // Static linking of OSG needs special macros |
| 73 | #ifdef OSG_LIBRARY_STATIC |
| 74 | #include <osgDB/Registry> |
| 75 | USE_GRAPHICSWINDOW(); |
| 76 | // Image formats |
| 77 | USE_OSGPLUGIN(bmp); |
| 78 | USE_OSGPLUGIN(dds); |
| 79 | USE_OSGPLUGIN(hdr); |
| 80 | USE_OSGPLUGIN(pic); |
| 81 | USE_OSGPLUGIN(pnm); |
| 82 | USE_OSGPLUGIN(rgb); |
| 83 | USE_OSGPLUGIN(tga); |
| 84 | #ifdef OSG_JPEG_ENABLED |
| 85 | USE_OSGPLUGIN(jpeg); |
| 86 | #endif |
| 87 | #ifdef OSG_PNG_ENABLED |
| 88 | USE_OSGPLUGIN(png); |
| 89 | #endif |
| 90 | #ifdef OSG_TIFF_ENABLED |
| 91 | USE_OSGPLUGIN(tiff); |
| 92 | #endif |
| 93 | // Model formats |
| 94 | USE_OSGPLUGIN(3ds); |
| 95 | USE_OSGPLUGIN(ac); |
| 96 | USE_OSGPLUGIN(ive); |
| 97 | USE_OSGPLUGIN(osg); |
| 98 | USE_OSGPLUGIN(txf); |
| 99 | #endif |
| 100 | |
| 101 | // fg_os implementation using OpenSceneGraph's osgViewer::Viewer class |
| 102 | // to create the graphics window and run the event/update/render loop. |
| 103 | |
| 104 | // |
| 105 | // fg_os implementation |
| 106 | // |
| 107 | |
| 108 | using namespace std; |
| 109 | using namespace flightgear; |
| 110 | using namespace osg; |
| 111 | |
| 112 | osg::ref_ptr<osgViewer::Viewer> viewer; |
| 113 | |
| 114 | static void setStereoMode(const char* mode) |
| 115 | { |
| 116 | DisplaySettings::StereoMode stereoMode = DisplaySettings::QUAD_BUFFER; |
| 117 | bool stereoOn = true; |
| 118 | |
| 119 | if (strcmp(mode, "QUAD_BUFFER") == 0) { |
| 120 | stereoMode = DisplaySettings::QUAD_BUFFER; |
| 121 | } else if (strcmp(mode, "ANAGLYPHIC") == 0) { |
| 122 | stereoMode = DisplaySettings::ANAGLYPHIC; |
| 123 | } else if (strcmp(mode, "HORIZONTAL_SPLIT") == 0) { |
| 124 | stereoMode = DisplaySettings::HORIZONTAL_SPLIT; |
| 125 | } else if (strcmp(mode, "VERTICAL_SPLIT") == 0) { |
| 126 | stereoMode = DisplaySettings::VERTICAL_SPLIT; |
| 127 | } else if (strcmp(mode, "LEFT_EYE") == 0) { |
| 128 | stereoMode = DisplaySettings::LEFT_EYE; |
| 129 | } else if (strcmp(mode, "RIGHT_EYE") == 0) { |
| 130 | stereoMode = DisplaySettings::RIGHT_EYE; |
| 131 | } else if (strcmp(mode, "HORIZONTAL_INTERLACE") == 0) { |
| 132 | stereoMode = DisplaySettings::HORIZONTAL_INTERLACE; |
| 133 | } else if (strcmp(mode, "VERTICAL_INTERLACE") == 0) { |
| 134 | stereoMode = DisplaySettings::VERTICAL_INTERLACE; |
| 135 | } else if (strcmp(mode, "CHECKERBOARD") == 0) { |
| 136 | stereoMode = DisplaySettings::CHECKERBOARD; |
| 137 | } else { |
| 138 | stereoOn = false; |
| 139 | } |
| 140 | DisplaySettings::instance()->setStereo(stereoOn); |
| 141 | DisplaySettings::instance()->setStereoMode(stereoMode); |
| 142 | } |
| 143 | |
| 144 | static const char* getStereoMode() |
| 145 | { |
| 146 | DisplaySettings::StereoMode stereoMode = DisplaySettings::instance()->getStereoMode(); |
| 147 | bool stereoOn = DisplaySettings::instance()->getStereo(); |
| 148 | if (!stereoOn) return "OFF"; |
| 149 | if (stereoMode == DisplaySettings::QUAD_BUFFER) { |
| 150 | return "QUAD_BUFFER"; |
| 151 | } else if (stereoMode == DisplaySettings::ANAGLYPHIC) { |
| 152 | return "ANAGLYPHIC"; |
| 153 | } else if (stereoMode == DisplaySettings::HORIZONTAL_SPLIT) { |
| 154 | return "HORIZONTAL_SPLIT"; |
| 155 | } else if (stereoMode == DisplaySettings::VERTICAL_SPLIT) { |
| 156 | return "VERTICAL_SPLIT"; |
| 157 | } else if (stereoMode == DisplaySettings::LEFT_EYE) { |
| 158 | return "LEFT_EYE"; |
| 159 | } else if (stereoMode == DisplaySettings::RIGHT_EYE) { |
| 160 | return "RIGHT_EYE"; |
| 161 | } else if (stereoMode == DisplaySettings::HORIZONTAL_INTERLACE) { |
| 162 | return "HORIZONTAL_INTERLACE"; |
| 163 | } else if (stereoMode == DisplaySettings::VERTICAL_INTERLACE) { |
| 164 | return "VERTICAL_INTERLACE"; |
| 165 | } else if (stereoMode == DisplaySettings::CHECKERBOARD) { |
| 166 | return "CHECKERBOARD"; |
| 167 | } |
| 168 | return "OFF"; |
| 169 | } |
| 170 | |
| 171 | class NotifyLevelListener : public SGPropertyChangeListener |
| 172 | { |
| 173 | public: |
| 174 | void valueChanged(SGPropertyNode* node) |
| 175 | { |
| 176 | osg::NotifySeverity severity = osg::getNotifyLevel(); |
| 177 | string val = simgear::strutils::lowercase(node->getStringValue()); |
| 178 | |
| 179 | if (val == "fatal") { |
| 180 | severity = osg::FATAL; |
| 181 | } else if (val == "warn") { |
| 182 | severity = osg::WARN; |
| 183 | } else if (val == "notice") { |
| 184 | severity = osg::NOTICE; |
| 185 | } else if (val == "info") { |
| 186 | severity = osg::INFO; |
| 187 | } else if ((val == "debug") || (val == "debug-info")) { |
| 188 | severity = osg::DEBUG_INFO; |
| 189 | } |
| 190 | |
| 191 | osg::setNotifyLevel(severity); |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | void updateOSGNotifyLevel() |
| 196 | { |
| 197 | } |
| 198 | |
| 199 | void fgOSOpenWindow(bool stencil) |
| 200 | { |
| 201 | osg::setNotifyHandler(new SGNotifyHandler); |
| 202 | |
| 203 | auto composite_viewer = dynamic_cast<osgViewer::CompositeViewer*>( |
| 204 | globals->get_renderer()->getViewerBase()); |
| 205 | if (0) { |
| 206 | } else if (composite_viewer) { |
| 207 | /* We are using CompositeViewer. */ |
| 208 | SG_LOG(SG_VIEW, SG_DEBUG, "Using CompositeViewer"); |
| 209 | osgViewer::ViewerBase* viewer = globals->get_renderer()->getViewerBase(); |
| 210 | SG_LOG(SG_VIEW, SG_DEBUG, "Creating osgViewer::View"); |
| 211 | osgViewer::View* view = new osgViewer::View; |
| 212 | view->setFrameStamp(composite_viewer->getFrameStamp()); |
| 213 | globals->get_renderer()->setView(view); |
| 214 | assert(globals->get_renderer()->getView() == view); |
| 215 | view->setDatabasePager(FGScenery::getPagerSingleton()); |
| 216 | |
| 217 | // https://www.mail-archive.com/[email protected]/msg29820.html |
| 218 | view->getDatabasePager()->setUnrefImageDataAfterApplyPolicy(true, false); |
| 219 | // osg::GraphicsContext::createNewContextID(); |
| 220 | |
| 221 | //viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); |
| 222 | |
| 223 | std::string mode; |
| 224 | mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded"); |
| 225 | SG_LOG(SG_VIEW, SG_INFO, "mode=" << mode); |
| 226 | if (mode == "AutomaticSelection") |
| 227 | viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection); |
| 228 | else if (mode == "CullDrawThreadPerContext") |
| 229 | viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext); |
| 230 | else if (mode == "DrawThreadPerContext") |
| 231 | viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); |
| 232 | else if (mode == "CullThreadPerCameraDrawThreadPerContext") |
| 233 | viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); |
| 234 | else |
| 235 | viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); |
| 236 | |
| 237 | WindowBuilder::initWindowBuilder(stencil); |
| 238 | CameraGroup::buildDefaultGroup(view); |
| 239 | |
| 240 | FGEventHandler* manipulator = globals->get_renderer()->getEventHandler(); |
| 241 | WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA(); |
| 242 | if (wsa->windows.size() != 1) { |
| 243 | manipulator->setResizable(false); |
| 244 | } |
| 245 | view->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED); |
| 246 | view->addEventHandler(manipulator); |
| 247 | // Let FG handle the escape key with a confirmation |
| 248 | viewer->setKeyEventSetsDone(0); |
| 249 | // The viewer won't start without some root. |
| 250 | view->setSceneData(new osg::Group); |
| 251 | globals->get_renderer()->setView(view); |
| 252 | } else { |
| 253 | /* Not using CompositeViewer. */ |
| 254 | SG_LOG(SG_VIEW, SG_DEBUG, "Not CompositeViewer."); |
| 255 | SG_LOG(SG_VIEW, SG_DEBUG, "Creating osgViewer::Viewer"); |
| 256 | viewer = new osgViewer::Viewer; |
| 257 | viewer->setDatabasePager(FGScenery::getPagerSingleton()); |
| 258 | |
| 259 | std::string mode; |
| 260 | mode = fgGetString("/sim/rendering/multithreading-mode", "SingleThreaded"); |
| 261 | flightgear::addSentryTag("osg-thread-mode", mode); |
| 262 | |
| 263 | if (mode == "AutomaticSelection") |
| 264 | viewer->setThreadingModel(osgViewer::Viewer::AutomaticSelection); |
| 265 | else if (mode == "CullDrawThreadPerContext") |
| 266 | viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext); |
| 267 | else if (mode == "DrawThreadPerContext") |
| 268 | viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); |
| 269 | else if (mode == "CullThreadPerCameraDrawThreadPerContext") |
| 270 | viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); |
| 271 | else |
| 272 | viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); |
| 273 | WindowBuilder::initWindowBuilder(stencil); |
| 274 | CameraGroup::buildDefaultGroup(viewer.get()); |
| 275 | |
| 276 | FGEventHandler* manipulator = globals->get_renderer()->getEventHandler(); |
| 277 | WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA(); |
| 278 | if (wsa->windows.size() != 1) { |
| 279 | manipulator->setResizable(false); |
| 280 | } |
| 281 | viewer->getCamera()->setProjectionResizePolicy(osg::Camera::FIXED); |
| 282 | viewer->addEventHandler(manipulator); |
| 283 | // Let FG handle the escape key with a confirmation |
| 284 | viewer->setKeyEventSetsDone(0); |
| 285 | // The viewer won't start without some root. |
| 286 | viewer->setSceneData(new osg::Group); |
| 287 | globals->get_renderer()->setView(viewer.get()); |
| 288 | } |
| 289 | } |
| 290 | SGPropertyNode *simHost = 0, *simFrameCount, *simTotalHostTime, *simFrameResetCount, *frameWait; |
| 291 | |
| 292 | // Getter/Setter to work around lack of unsigned int properties. Note that we have a minimum of 1 DB thread as otherwise |
| 293 | // nothing will be loaded. We also force the number of HTTP threads to 0, as we don't use them. |
| 294 | inline int getNumDatabaseThreads() { return DisplaySettings::instance()->getNumOfDatabaseThreadsHint(); } |
| 295 | inline void setNumDatabaseThreads(int threads) { DisplaySettings::instance()->setNumOfDatabaseThreadsHint(max(threads, 1)); DisplaySettings::instance()->setNumOfHttpDatabaseThreadsHint(0); } |
| 296 | |
| 297 | void fgOSResetProperties() |
| 298 | { |
| 299 | SGPropertyNode* osgLevel = fgGetNode("/sim/rendering/osg-notify-level", true); |
| 300 | simTotalHostTime = fgGetNode("/sim/rendering/sim-host-total-ms", true); |
| 301 | simHost = fgGetNode("/sim/rendering/sim-host-avg-ms", true); |
| 302 | simFrameCount = fgGetNode("/sim/rendering/sim-frame-count", true); |
| 303 | simFrameResetCount = fgGetNode("/sim/rendering/sim-frame-count-reset", true); |
| 304 | frameWait = fgGetNode("/sim/time/frame-wait-ms", true); |
| 305 | simFrameResetCount->setBoolValue(false); |
| 306 | NotifyLevelListener* l = new NotifyLevelListener; |
| 307 | globals->addListenerToCleanup(l); |
| 308 | osgLevel->addChangeListener(l, true); |
| 309 | |
| 310 | osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault()); |
| 311 | if (guiCamera) { |
| 312 | Viewport* guiViewport = guiCamera->getViewport(); |
| 313 | fgSetInt("/sim/startup/xsize", guiViewport->width()); |
| 314 | fgSetInt("/sim/startup/ysize", guiViewport->height()); |
| 315 | } |
| 316 | |
| 317 | DisplaySettings* displaySettings = DisplaySettings::instance(); |
| 318 | fgTie("/sim/rendering/osg-displaysettings/split-stereo-autoadjust-aspect-ratio", displaySettings, &DisplaySettings::getSplitStereoAutoAdjustAspectRatio, &DisplaySettings::setSplitStereoAutoAdjustAspectRatio); |
| 319 | fgTie("/sim/rendering/osg-displaysettings/eye-separation", displaySettings, &DisplaySettings::getEyeSeparation, &DisplaySettings::setEyeSeparation); |
| 320 | fgTie("/sim/rendering/osg-displaysettings/screen-distance", displaySettings, &DisplaySettings::getScreenDistance, &DisplaySettings::setScreenDistance); |
| 321 | fgTie("/sim/rendering/osg-displaysettings/screen-width", displaySettings, &DisplaySettings::getScreenWidth, &DisplaySettings::setScreenWidth); |
| 322 | fgTie("/sim/rendering/osg-displaysettings/screen-height", displaySettings, &DisplaySettings::getScreenHeight, &DisplaySettings::setScreenHeight); |
| 323 | fgTie("/sim/rendering/osg-displaysettings/stereo-mode", getStereoMode, setStereoMode); |
| 324 | fgTie("/sim/rendering/osg-displaysettings/double-buffer", displaySettings, &DisplaySettings::getDoubleBuffer, &DisplaySettings::setDoubleBuffer); |
| 325 | fgTie("/sim/rendering/osg-displaysettings/depth-buffer", displaySettings, &DisplaySettings::getDepthBuffer, &DisplaySettings::setDepthBuffer); |
| 326 | fgTie("/sim/rendering/osg-displaysettings/rgb", displaySettings, &DisplaySettings::getRGB, &DisplaySettings::setRGB); |
| 327 | |
| 328 | fgTie("/sim/rendering/database-pager/threads", &getNumDatabaseThreads, &setNumDatabaseThreads); |
| 329 | |
| 330 | #ifdef ENABLE_OSGXR |
| 331 | fgSetBool("/sim/vr/built", true); |
| 332 | #else |
| 333 | fgSetBool("/sim/vr/built", false); |
| 334 | #endif |
| 335 | } |
| 336 | |
| 337 | |
| 338 | static int status = 0; |
| 339 | |
| 340 | void fgOSExit(int code) |
| 341 | { |
| 342 | FGRenderer* renderer = globals->get_renderer(); |
| 343 | renderer->getViewerBase()->setDone(true); |
| 344 | renderer->getView()->getDatabasePager()->cancel(); |
| 345 | status = code; |
| 346 | |
| 347 | // otherwise we crash if OSG does logging during static destruction, eg |
| 348 | // GraphicsWindowX11, since OSG statics may have been created before the |
| 349 | // sglog static, despite our best efforts in boostrap.cxx |
| 350 | osg::setNotifyHandler(new osg::StandardNotifyHandler); |
| 351 | } |
| 352 | SGTimeStamp _lastUpdate; |
| 353 | |
| 354 | static void ShowAffinities() |
| 355 | { |
| 356 | #ifdef __linux__ |
| 357 | char command[1024]; |
| 358 | snprintf(command, sizeof(command), "for i in `ls /proc/%i/task/`; do taskset -p $i; done 1>&2", getpid()); |
| 359 | SG_LOG(SG_VIEW, SG_ALERT, "Running: " << command); |
| 360 | system(command); |
| 361 | #endif |
| 362 | } |
| 363 | |
| 364 | #ifdef __linux__ |
| 365 | static std::ostream& operator<<(std::ostream& out, const cpu_set_t& mask) |
| 366 | { |
| 367 | out << "0x"; |
| 368 | unsigned char* mask2 = (unsigned char*)&mask; |
| 369 | for (unsigned i = 0; i < sizeof(mask); ++i) { |
| 370 | char buffer[8]; |
| 371 | snprintf(buffer, sizeof(buffer), "%02x", (unsigned)mask2[i]); |
| 372 | out << buffer; |
| 373 | } |
| 374 | return out; |
| 375 | } |
| 376 | #endif |
| 377 | |
| 378 | /* Listen to /sim/affinity-control and, on Linux only, responds to |
| 379 | value='clear' and 'revert': |
| 380 | |
| 381 | 'clear' |
| 382 | Stores current affinities for all thread then resets all affinities so |
| 383 | that all threads can run on any cpu core. |
| 384 | 'revert' |
| 385 | Restores thread affinities stored from previous 'clear'. |
| 386 | */ |
| 387 | struct AffinityControl : SGPropertyChangeListener { |
| 388 | AffinityControl() |
| 389 | { |
| 390 | m_node = globals->get_props()->getNode("/sim/affinity-control", true /*create*/); |
| 391 | m_node->addChangeListener(this); |
| 392 | } |
| 393 | void valueChanged(SGPropertyNode* node) override |
| 394 | { |
| 395 | #ifdef __linux__ |
| 396 | std::string s = m_node->getStringValue(); |
| 397 | if (s == m_state) { |
| 398 | SG_LOG(SG_VIEW, SG_ALERT, "Ignoring m_node=" << s << " because same as m_state."); |
| 399 | } else if (s == "clear") { |
| 400 | char buffer[64]; |
| 401 | snprintf(buffer, sizeof(buffer), "/proc/%i/task", getpid()); |
| 402 | SGPath path(buffer); |
| 403 | simgear::Dir dir(path); |
| 404 | m_thread_masks.clear(); |
| 405 | simgear::PathList pids = dir.children( |
| 406 | simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT); |
| 407 | for (SGPath path : pids) { |
| 408 | std::string leaf = path.file(); |
| 409 | int pid = atoi(leaf.c_str()); |
| 410 | cpu_set_t mask; |
| 411 | int e = sched_getaffinity(pid, sizeof(mask), &mask); |
| 412 | SG_LOG(SG_VIEW, SG_ALERT, "Called sched_getaffinity()" |
| 413 | << " pid=" << pid << " => e=" << e << " mask=" << mask); |
| 414 | if (!e) { |
| 415 | m_thread_masks[pid] = mask; |
| 416 | memset(&mask, 255, sizeof(mask)); |
| 417 | e = sched_setaffinity(pid, sizeof(mask), &mask); |
| 418 | SG_LOG(SG_VIEW, SG_ALERT, "Called sched_setaffinity()" |
| 419 | << " pid=" << pid << " => e=" << e << " mask=" << mask); |
| 420 | //assert(!e); |
| 421 | } |
| 422 | } |
| 423 | m_state = s; |
| 424 | } else if (s == "revert") { |
| 425 | for (auto it : m_thread_masks) { |
| 426 | pid_t pid = it.first; |
| 427 | cpu_set_t mask = it.second; |
| 428 | int e = sched_setaffinity(pid, sizeof(mask), &mask); |
| 429 | SG_LOG(SG_VIEW, SG_ALERT, "Called sched_setaffinity()" |
| 430 | << " pid=" << pid << " => e=" << e << " mask=" << mask); |
| 431 | //assert(!e); |
| 432 | } |
| 433 | m_thread_masks.clear(); |
| 434 | m_state = s; |
| 435 | } else { |
| 436 | SG_LOG(SG_VIEW, SG_ALERT, "Unrecognised m_node=" << s); |
| 437 | } |
| 438 | #endif |
| 439 | } |
| 440 | SGPropertyNode_ptr m_node; |
| 441 | std::string m_state; |
| 442 | #ifdef __linux__ |
| 443 | std::map<int, cpu_set_t> m_thread_masks; |
| 444 | #endif |
| 445 | }; |
| 446 | |
| 447 | |
| 448 | int fgOSMainLoop() |
| 449 | { |
| 450 | AffinityControl affinity_control; |
| 451 | osgViewer::ViewerBase* viewer_base = globals->get_renderer()->getViewerBase(); |
| 452 | viewer_base->setReleaseContextAtEndOfFrameHint(false); |
| 453 | if (!viewer_base->isRealized()) { |
| 454 | viewer_base->realize(); |
| 455 | std::string affinity = fgGetString("/sim/thread-cpu-affinity"); |
| 456 | SG_LOG(SG_VIEW, SG_ALERT, "affinity=" << affinity); |
| 457 | if (affinity != "") { |
| 458 | ShowAffinities(); |
| 459 | if (affinity == "osg") { |
| 460 | SG_LOG(SG_VIEW, SG_ALERT, "Resetting affinity of current thread getpid()=" << getpid()); |
| 461 | OpenThreads::Affinity affinity; |
| 462 | OpenThreads::SetProcessorAffinityOfCurrentThread(affinity); |
| 463 | ShowAffinities(); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | while (!viewer_base->done()) { |
| 469 | fgIdleHandler idleFunc = globals->get_renderer()->getEventHandler()->getIdleHandler(); |
| 470 | if (idleFunc) { |
| 471 | _lastUpdate.stamp(); |
| 472 | (*idleFunc)(); |
| 473 | if (fgGetBool("/sim/position-finalized", false)) { |
| 474 | if (simHost && simFrameCount && simTotalHostTime && simFrameResetCount) { |
| 475 | int curFrameCount = simFrameCount->getIntValue(); |
| 476 | double totalSimTime = simTotalHostTime->getDoubleValue(); |
| 477 | if (simFrameResetCount->getBoolValue()) { |
| 478 | curFrameCount = 0; |
| 479 | totalSimTime = 0; |
| 480 | simFrameResetCount->setBoolValue(false); |
| 481 | } |
| 482 | double lastSimFrame_ms = _lastUpdate.elapsedMSec(); |
| 483 | double idle_wait = 0; |
| 484 | if (frameWait) |
| 485 | idle_wait = frameWait->getDoubleValue(); |
| 486 | if (lastSimFrame_ms > 0) { |
| 487 | totalSimTime += lastSimFrame_ms - idle_wait; |
| 488 | simTotalHostTime->setDoubleValue(totalSimTime); |
| 489 | curFrameCount++; |
| 490 | simFrameCount->setIntValue(curFrameCount); |
| 491 | simHost->setDoubleValue(totalSimTime / curFrameCount); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | globals->get_renderer()->update(); |
| 497 | #ifdef ENABLE_OSGXR |
| 498 | VRManager::instance()->update(); |
| 499 | #endif |
| 500 | viewer_base->frame(globals->get_sim_time_sec()); |
| 501 | } |
| 502 | |
| 503 | flightgear::addSentryBreadcrumb("main loop exited", "info"); |
| 504 | return status; |
| 505 | } |
| 506 | |
| 507 | int fgGetKeyModifiers() |
| 508 | { |
| 509 | FGRenderer* r = globals->get_renderer(); |
| 510 | if (!r || !r->getEventHandler()) { // happens during shutdown |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | return r->getEventHandler()->getCurrentModifiers(); |
| 515 | } |
| 516 | |
| 517 | void fgWarpMouse(int x, int y) |
| 518 | { |
| 519 | warpGUIPointer(CameraGroup::getDefault(), x, y); |
| 520 | } |
| 521 | |
| 522 | void fgOSInit(int* argc, char** argv) |
| 523 | { |
| 524 | // stock OSG windows are not Hi-DPI aware |
| 525 | fgSetDouble("/sim/rendering/gui-pixel-ratio", 1.0); |
| 526 | |
| 527 | #if defined(SG_MAC) |
| 528 | cocoaRegisterTerminateHandler(); |
| 529 | #endif |
| 530 | |
| 531 | globals->get_renderer()->init(); |
| 532 | WindowSystemAdapter::setWSA(new WindowSystemAdapter); |
| 533 | } |
| 534 | |
| 535 | void fgOSCloseWindow() |
| 536 | { |
| 537 | // reset the cursor before we close the window |
| 538 | |
| 539 | fgSetMouseCursor(FGMouseCursor::CURSOR_ARROW); |
| 540 | |
| 541 | if (globals && globals->get_renderer()) { |
| 542 | osgViewer::ViewerBase* viewer_base = globals->get_renderer()->getViewerBase(); |
| 543 | if (viewer_base) { |
| 544 | // https://code.google.com/p/flightgear-bugs/issues/detail?id=1291 |
| 545 | // https://sourceforge.net/p/flightgear/codetickets/1830/ |
| 546 | // explicitly stop threading before we delete the renderer or |
| 547 | // viewMgr (which ultimately holds refs to the CameraGroup, and |
| 548 | // GraphicsContext) |
| 549 | viewer_base->stopThreading(); |
| 550 | } |
| 551 | } |
| 552 | FGScenery::resetPagerSingleton(); |
| 553 | flightgear::addSentryBreadcrumb("fgOSCloseWindow, clearing camera group", "info"); |
| 554 | flightgear::CameraGroup::setDefault(NULL); |
| 555 | WindowSystemAdapter::setWSA(NULL); |
| 556 | viewer = NULL; |
| 557 | } |
| 558 | |
| 559 | void fgOSFullScreen() |
| 560 | { |
| 561 | osgViewer::ViewerBase* viewer_base = globals->get_renderer()->getViewerBase(); |
| 562 | std::vector<osgViewer::GraphicsWindow*> windows; |
| 563 | viewer_base->getWindows(windows); |
| 564 | |
| 565 | if (windows.empty()) |
| 566 | return; // Huh?!? |
| 567 | |
| 568 | /* Toggling window fullscreen is only supported for the main GUI window. |
| 569 | * The other windows should use fixed setup from the camera.xml file anyway. */ |
| 570 | osgViewer::GraphicsWindow* window = windows[0]; |
| 571 | |
| 572 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
| 573 | if (wsi == NULL) { |
| 574 | SG_LOG(SG_VIEW, SG_ALERT, "ERROR: No WindowSystemInterface available. Cannot toggle window fullscreen."); |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | static int previous_x = 0; |
| 579 | static int previous_y = 0; |
| 580 | static int previous_width = 800; |
| 581 | static int previous_height = 600; |
| 582 | |
| 583 | unsigned int screenWidth; |
| 584 | unsigned int screenHeight; |
| 585 | wsi->getScreenResolution(*(window->getTraits()), screenWidth, screenHeight); |
| 586 | |
| 587 | int x; |
| 588 | int y; |
| 589 | int width; |
| 590 | int height; |
| 591 | window->getWindowRectangle(x, y, width, height); |
| 592 | |
| 593 | /* Note: the simple "is window size == screen size" check to detect full screen state doesn't work with |
| 594 | * X screen servers in Xinerama mode, since the reported screen width (or height) exceeds the maximum width |
| 595 | * (or height) usable by a single window (Xserver automatically shrinks/moves the full screen window to fit a |
| 596 | * single display) - so we detect full screen mode using "WindowDecoration" state instead. |
| 597 | * "false" - even when a single window is display in fullscreen */ |
| 598 | //bool isFullScreen = x == 0 && y == 0 && width == (int)screenWidth && height == (int)screenHeight; |
| 599 | bool isFullScreen = !window->getWindowDecoration(); |
| 600 | |
| 601 | SG_LOG(SG_VIEW, SG_DEBUG, "Toggling fullscreen. Previous window rectangle (" << x << ", " << y << ") x (" << width << ", " << height << "), fullscreen: " << isFullScreen << ", number of screens: " << wsi->getNumScreens()); |
| 602 | if (isFullScreen) { |
| 603 | // limit x,y coordinates and window size to screen area |
| 604 | if (previous_x + previous_width > (int)screenWidth) |
| 605 | previous_x = 0; |
| 606 | if (previous_y + previous_height > (int)screenHeight) |
| 607 | previous_y = 0; |
| 608 | |
| 609 | // disable fullscreen mode, restore previous window size/coordinates |
| 610 | x = previous_x; |
| 611 | y = previous_y; |
| 612 | width = previous_width; |
| 613 | height = previous_height; |
| 614 | } else { |
| 615 | // remember previous setting |
| 616 | previous_x = x; |
| 617 | previous_y = y; |
| 618 | previous_width = width; |
| 619 | previous_height = height; |
| 620 | |
| 621 | // enable fullscreen mode, set new width/height |
| 622 | x = 0; |
| 623 | y = 0; |
| 624 | width = screenWidth; |
| 625 | height = screenHeight; |
| 626 | } |
| 627 | |
| 628 | // set xsize/ysize properties to adapt GUI planes |
| 629 | fgSetInt("/sim/startup/xsize", width); |
| 630 | fgSetInt("/sim/startup/ysize", height); |
| 631 | fgSetBool("/sim/startup/fullscreen", !isFullScreen); |
| 632 | |
| 633 | // reconfigure window |
| 634 | window->setWindowDecoration(isFullScreen); |
| 635 | window->setWindowRectangle(x, y, width, height); |
| 636 | window->grabFocusIfPointerInWindow(); |
| 637 | } |
| 638 | |
| 639 | void fgSetMouseCursor(FGMouseCursor::Cursor cursor) |
| 640 | { |
| 641 | FGMouseCursor::instance()->setCursor(cursor); |
| 642 | } |
| 643 | |
| 644 | FGMouseCursor::Cursor fgGetMouseCursor() |
| 645 | { |
| 646 | return FGMouseCursor::instance()->getCursor(); |
| 647 | } |