1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 
29 #ifndef __Ogre_Page_H__
30 #define __Ogre_Page_H__
31 
32 #include "OgrePagingPrerequisites.h"
33 #include "Threading/OgreThreadHeaders.h"
34 #include "OgreWorkQueue.h"
35 
36 
37 namespace Ogre
38 {
39     /** \addtogroup Optional
40     *  @{
41     */
42     /** \defgroup Paging Paging
43     *  Render large modular structures
44     *  @{
45     */
46 
47     /** Page class
48     */
49     class _OgrePagingExport Page : public WorkQueue::RequestHandler,
50         public WorkQueue::ResponseHandler, public PageAlloc
51     {
52     public:
53         typedef std::vector<PageContentCollection*> ContentCollectionList;
54     protected:
55         PageID mID;
56         PagedWorldSection* mParent;
57         unsigned long mFrameLastHeld;
58         ContentCollectionList mContentCollections;
59         uint16 mWorkQueueChannel;
60         bool mDeferredProcessInProgress;
61         bool mModified;
62 
63         SceneNode* mDebugNode;
64         void updateDebugDisplay();
65 
66         struct PageData : public PageAlloc
67         {
68             ContentCollectionList collectionsToAdd;
69         };
70         /// Structure for holding background page requests
71         struct PageRequest
72         {
73             Page* srcPage;
74             _OgrePagingExport friend std::ostream& operator<<(std::ostream& o, const PageRequest& r)
75             { return o; }
76 
PageRequestPageRequest77             PageRequest(Page* p): srcPage(p) {}
78         };
79         struct PageResponse
80         {
81             PageData* pageData;
82 
83             _OgrePagingExport friend std::ostream& operator<<(std::ostream& o, const PageResponse& r)
84             { return o; }
85 
PageResponsePageResponse86             PageResponse() : pageData(0) {}
87         };
88 
89 
90 
91         virtual bool prepareImpl(PageData* dataToPopulate);
92         virtual bool prepareImpl(StreamSerialiser& str, PageData* dataToPopulate);
93         virtual void loadImpl();
94 
95         String generateFilename() const;
96 
97     public:
98         static const uint32 CHUNK_ID;
99         static const uint16 CHUNK_VERSION;
100 
101         static const uint32 CHUNK_CONTENTCOLLECTION_DECLARATION_ID;
102 
103         Page(PageID pageID, PagedWorldSection* parent);
104         virtual ~Page();
105 
106         PageManager* getManager() const;
107         SceneManager* getSceneManager() const;
108 
109         /// If true, it's not safe to access this Page at this time, contents may be changing
isDeferredProcessInProgress()110         bool isDeferredProcessInProgress() const { return mDeferredProcessInProgress; }
111 
112         /// Get the ID of this page, unique within the parent
getID()113         virtual PageID getID() const { return mID; }
114         /// Get the PagedWorldSection this page belongs to
getParentSection()115         virtual PagedWorldSection* getParentSection() const { return mParent; }
116         /** Get the frame number in which this Page was last loaded or held.
117         @remarks
118             A Page that has not been requested to be loaded or held in the recent
119             past will be a candidate for removal.
120         */
getFrameLastHeld()121         virtual unsigned long getFrameLastHeld() { return mFrameLastHeld; }
122         /// 'Touch' the page to let it know it's being used
123         virtual void touch();
124 
125         /** Load this page.
126         @param synchronous Whether to force this to happen synchronously.
127         */
128         virtual void load(bool synchronous);
129         /** Unload this page.
130         */
131         virtual void unload();
132 
133 
134         /** Returns whether this page was 'held' in the last frame, that is
135             was it either directly needed, or requested to stay in memory (held - as
136             in a buffer region for example). If not, this page is eligible for
137             removal.
138         */
139         virtual bool isHeld() const;
140 
141         /// Save page data to an automatically generated file name
142         virtual void save();
143         /// Save page data to a file
144         virtual void save(const String& filename);
145         /// Save page data to a serialiser
146         virtual void save(StreamSerialiser& stream);
147 
148         /// Called when the frame starts
149         virtual void frameStart(Real timeSinceLastFrame);
150         /// Called when the frame ends
151         virtual void frameEnd(Real timeElapsed);
152         /// Notify a section of the current camera
153         virtual void notifyCamera(Camera* cam);
154 
155         /** Create a new PageContentCollection within this page.
156         This is equivalent to calling PageManager::createContentCollection and
157         then attachContentCollection.
158         @param typeName The name of the type of content collection (see PageManager::getContentCollectionFactories)
159         */
160         virtual PageContentCollection* createContentCollection(const String& typeName);
161 
162         /** Destroy a PageContentCollection within this page.
163         This is equivalent to calling detachContentCollection and
164             PageManager::destroyContentCollection.
165         */
166         virtual void destroyContentCollection(PageContentCollection* coll);
167         /** Destroy all PageContentCollections within this page.
168         */
169         virtual void destroyAllContentCollections();
170         /// Get the number of content collections
171         virtual size_t getContentCollectionCount() const;
172         /// Get a content collection
173         virtual PageContentCollection* getContentCollection(size_t index);
174         /// Get the list of content collections
175         const ContentCollectionList& getContentCollectionList() const;
176 
177         /// WorkQueue::RequestHandler override
178         bool canHandleRequest(const WorkQueue::Request* req, const WorkQueue* srcQ);
179         /// WorkQueue::RequestHandler override
180         WorkQueue::Response* handleRequest(const WorkQueue::Request* req, const WorkQueue* srcQ);
181         /// WorkQueue::ResponseHandler override
182         bool canHandleResponse(const WorkQueue::Response* res, const WorkQueue* srcQ);
183         /// WorkQueue::ResponseHandler override
184         void handleResponse(const WorkQueue::Response* res, const WorkQueue* srcQ);
185 
186 
187         /// Tell the page that it is modified
_notifyModified()188         void _notifyModified() { mModified = true; }
isModified()189         bool isModified() const { return mModified; }
190 
191         static const uint16 WORKQUEUE_PREPARE_REQUEST;
192         static const uint16 WORKQUEUE_CHANGECOLLECTION_REQUEST;
193 
194         /** Function for writing to a stream.
195         */
196         _OgrePagingExport friend std::ostream& operator <<( std::ostream& o, const Page& p );
197 
198 
199 
200     };
201 
202     /** @} */
203     /** @} */
204 }
205 
206 #endif
207