| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGDB_IMAGEPAGER |
|---|
| 15 | #define OSGDB_IMAGEPAGER 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Image> |
|---|
| 18 | #include <osg/NodeVisitor> |
|---|
| 19 | #include <osg/observer_ptr> |
|---|
| 20 | #include <osg/OperationThread> |
|---|
| 21 | #include <osg/FrameStamp> |
|---|
| 22 | |
|---|
| 23 | #include <OpenThreads/Mutex> |
|---|
| 24 | |
|---|
| 25 | #include <osgDB/ReaderWriter> |
|---|
| 26 | #include <osgDB/Options> |
|---|
| 27 | |
|---|
| 28 | namespace osgDB |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | class OSGDB_EXPORT ImagePager : public osg::NodeVisitor::ImageRequestHandler |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | |
|---|
| 35 | ImagePager(); |
|---|
| 36 | |
|---|
| 37 | class OSGDB_EXPORT ImageThread : public osg::Referenced, public OpenThreads::Thread |
|---|
| 38 | { |
|---|
| 39 | public: |
|---|
| 40 | |
|---|
| 41 | enum Mode |
|---|
| 42 | { |
|---|
| 43 | HANDLE_ALL_REQUESTS, |
|---|
| 44 | HANDLE_NON_HTTP, |
|---|
| 45 | HANDLE_ONLY_HTTP |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | ImageThread(ImagePager* pager, Mode mode, const std::string& name); |
|---|
| 49 | |
|---|
| 50 | ImageThread(const ImageThread& dt, ImagePager* pager); |
|---|
| 51 | |
|---|
| 52 | void setDone(bool done) { _done = done; } |
|---|
| 53 | bool getDone() const { return _done; } |
|---|
| 54 | |
|---|
| 55 | virtual int cancel(); |
|---|
| 56 | |
|---|
| 57 | virtual void run(); |
|---|
| 58 | |
|---|
| 59 | protected: |
|---|
| 60 | |
|---|
| 61 | virtual ~ImageThread(); |
|---|
| 62 | |
|---|
| 63 | bool _done; |
|---|
| 64 | Mode _mode; |
|---|
| 65 | ImagePager* _pager; |
|---|
| 66 | std::string _name; |
|---|
| 67 | }; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | ImageThread* getImageThread(unsigned int i) { return _imageThreads[i].get(); } |
|---|
| 71 | |
|---|
| 72 | const ImageThread* getImageThread(unsigned int i) const { return _imageThreads[i].get(); } |
|---|
| 73 | |
|---|
| 74 | unsigned int getNumImageThreads() const { return _imageThreads.size(); } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | void setPreLoadTime(double preLoadTime) { _preLoadTime=preLoadTime; } |
|---|
| 78 | virtual double getPreLoadTime() const { return _preLoadTime; } |
|---|
| 79 | |
|---|
| 80 | virtual osg::Image* readImageFile(const std::string& fileName); |
|---|
| 81 | |
|---|
| 82 | virtual void requestImageFile(const std::string& fileName,osg::Object* attachmentPoint, int attachmentIndex, double timeToMergeBy, const osg::FrameStamp* framestamp); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | /** Return true if there are pending updates to the scene graph that require a call to updateSceneGraph(double). */ |
|---|
| 86 | virtual bool requiresUpdateSceneGraph() const; |
|---|
| 87 | |
|---|
| 88 | /** Merge the changes to the scene graph. */ |
|---|
| 89 | virtual void updateSceneGraph(const osg::FrameStamp &frameStamp); |
|---|
| 90 | |
|---|
| 91 | int cancel(); |
|---|
| 92 | |
|---|
| 93 | protected: |
|---|
| 94 | |
|---|
| 95 | virtual ~ImagePager(); |
|---|
| 96 | // forward declare |
|---|
| 97 | struct RequestQueue; |
|---|
| 98 | |
|---|
| 99 | struct SortFileRequestFunctor; |
|---|
| 100 | friend struct SortFileRequestFunctor; |
|---|
| 101 | |
|---|
| 102 | struct ImageRequest : public osg::Referenced |
|---|
| 103 | { |
|---|
| 104 | ImageRequest(): |
|---|
| 105 | osg::Referenced(true), |
|---|
| 106 | _timeToMergeBy(0.0), |
|---|
| 107 | _attachmentIndex(-1) {} |
|---|
| 108 | |
|---|
| 109 | double _timeToMergeBy; |
|---|
| 110 | std::string _fileName; |
|---|
| 111 | osg::ref_ptr<Options> _loadOptions; |
|---|
| 112 | osg::observer_ptr<osg::Object> _attachmentPoint; |
|---|
| 113 | int _attachmentIndex; |
|---|
| 114 | osg::ref_ptr<osg::Image> _loadedImage; |
|---|
| 115 | RequestQueue* _requestQueue; |
|---|
| 116 | |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | struct RequestQueue : public osg::Referenced |
|---|
| 120 | { |
|---|
| 121 | typedef std::vector< osg::ref_ptr<ImageRequest> > RequestList; |
|---|
| 122 | |
|---|
| 123 | void sort(); |
|---|
| 124 | |
|---|
| 125 | RequestList _requestList; |
|---|
| 126 | OpenThreads::Mutex _requestMutex; |
|---|
| 127 | }; |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | struct ReadQueue : public RequestQueue |
|---|
| 131 | { |
|---|
| 132 | ReadQueue(ImagePager* pager, const std::string& name); |
|---|
| 133 | |
|---|
| 134 | void block() { _block->block(); } |
|---|
| 135 | |
|---|
| 136 | void release() { _block->release(); } |
|---|
| 137 | |
|---|
| 138 | void updateBlock() |
|---|
| 139 | { |
|---|
| 140 | _block->set((!_requestList.empty() || !_pager->_databasePagerThreadPaused)); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | void clear(); |
|---|
| 144 | |
|---|
| 145 | void add(ImageRequest* imageRequest); |
|---|
| 146 | |
|---|
| 147 | void takeFirst(osg::ref_ptr<ImageRequest>& databaseRequest); |
|---|
| 148 | |
|---|
| 149 | osg::ref_ptr<osg::RefBlock> _block; |
|---|
| 150 | |
|---|
| 151 | ImagePager* _pager; |
|---|
| 152 | std::string _name; |
|---|
| 153 | }; |
|---|
| 154 | |
|---|
| 155 | OpenThreads::Mutex _run_mutex; |
|---|
| 156 | bool _startThreadCalled; |
|---|
| 157 | |
|---|
| 158 | bool _done; |
|---|
| 159 | bool _databasePagerThreadPaused; |
|---|
| 160 | |
|---|
| 161 | osg::ref_ptr<ReadQueue> _readQueue; |
|---|
| 162 | |
|---|
| 163 | typedef std::vector< osg::ref_ptr<ImageThread> > ImageThreads; |
|---|
| 164 | ImageThreads _imageThreads; |
|---|
| 165 | |
|---|
| 166 | osg::ref_ptr<RequestQueue> _completedQueue; |
|---|
| 167 | |
|---|
| 168 | double _preLoadTime; |
|---|
| 169 | }; |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | #endif |
|---|
| 175 | |
|---|