Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/display/graphicsEngine.h

Go to the documentation of this file.
00001 // Filename: graphicsEngine.h
00002 // Created by:  drose (24Feb02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #ifndef GRAPHICSENGINE_H
00020 #define GRAPHICSENGINE_H
00021 
00022 #include "pandabase.h"
00023 #include "graphicsWindow.h"
00024 #include "frameBufferProperties.h"
00025 #include "graphicsThreadingModel.h"
00026 #include "sceneSetup.h"
00027 #include "pointerTo.h"
00028 #include "thread.h"
00029 #include "pmutex.h"
00030 #include "conditionVar.h"
00031 #include "pset.h"
00032 #include "pStatCollector.h"
00033 
00034 class Pipeline;
00035 class DisplayRegion;
00036 class GraphicsPipe;
00037 class FrameBufferProperties;
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //       Class : GraphicsEngine
00041 // Description : This class is the main interface to controlling the
00042 //               render process.  There is typically only one
00043 //               GraphicsEngine in an application, and it synchronizes
00044 //               rendering to all all of the active windows; although
00045 //               it is possible to have multiple GraphicsEngine
00046 //               objects if multiple synchronicity groups are
00047 //               required.
00048 //
00049 //               The GraphicsEngine is responsible for managing the
00050 //               various cull and draw threads.  The application
00051 //               simply calls engine->render_frame() and considers it
00052 //               done.
00053 ////////////////////////////////////////////////////////////////////
00054 class EXPCL_PANDA GraphicsEngine {
00055 PUBLISHED:
00056   GraphicsEngine(Pipeline *pipeline = NULL);
00057   ~GraphicsEngine();
00058 
00059   void set_frame_buffer_properties(const FrameBufferProperties &properties);
00060   FrameBufferProperties get_frame_buffer_properties() const; 
00061 
00062   void set_threading_model(const GraphicsThreadingModel &threading_model);
00063   GraphicsThreadingModel get_threading_model() const;
00064 
00065   INLINE void set_auto_flip(bool auto_flip);
00066   INLINE bool get_auto_flip() const;
00067 
00068   INLINE PT(GraphicsStateGuardian) make_gsg(GraphicsPipe *pipe);
00069   PT(GraphicsStateGuardian) make_gsg(GraphicsPipe *pipe,
00070                                      const FrameBufferProperties &properties,
00071                                      const GraphicsThreadingModel &threading_model);
00072 
00073   INLINE GraphicsWindow *make_window(GraphicsPipe *pipe,
00074                                      GraphicsStateGuardian *gsg);
00075   GraphicsWindow *make_window(GraphicsPipe *pipe,
00076                               GraphicsStateGuardian *gsg,
00077                               const GraphicsThreadingModel &threading_model);
00078   bool remove_window(GraphicsWindow *window);
00079   void remove_all_windows();
00080   bool is_empty() const;
00081 
00082   void render_frame();
00083   void sync_frame();
00084   void flip_frame();
00085   
00086   void render_subframe(GraphicsStateGuardian *gsg, DisplayRegion *dr,
00087                        bool cull_sorting);
00088 
00089 private:
00090   typedef pset< PT(GraphicsWindow) > Windows;
00091   typedef pset< PT(GraphicsStateGuardian) > GSGs;
00092 
00093   void cull_and_draw_together(const Windows &wlist);
00094   void cull_and_draw_together(GraphicsStateGuardian *gsg, DisplayRegion *dr);
00095 
00096   void cull_bin_draw(const Windows &wlist);
00097   void cull_bin_draw(GraphicsStateGuardian *gsg, DisplayRegion *dr);
00098 
00099   void process_events(const GraphicsEngine::Windows &wlist);
00100   void flip_windows(const GraphicsEngine::Windows &wlist);
00101   void do_sync_frame();
00102   void do_flip_frame();
00103   INLINE void close_gsg(GraphicsPipe *pipe, GraphicsStateGuardian *gsg);
00104 
00105   PT(SceneSetup) setup_scene(const NodePath &camera, 
00106                              GraphicsStateGuardian *gsg);
00107   void do_cull(CullHandler *cull_handler, SceneSetup *scene_setup,
00108                GraphicsStateGuardian *gsg);
00109   void do_draw(CullResult *cull_result, SceneSetup *scene_setup,
00110                GraphicsStateGuardian *gsg, DisplayRegion *dr);
00111 
00112   bool setup_gsg(GraphicsStateGuardian *gsg, SceneSetup *scene_setup);
00113 
00114   void do_remove_window(GraphicsWindow *window);
00115   void terminate_threads();
00116 
00117   // The WindowRenderer class records the stages of the pipeline that
00118   // each thread (including the main thread, a.k.a. "app") should
00119   // process, and the list of windows for each stage.
00120   class WindowRenderer {
00121   public:
00122     void add_gsg(GraphicsStateGuardian *gsg);
00123     void add_window(Windows &wlist, GraphicsWindow *window);
00124     void remove_window(GraphicsWindow *window);
00125     void do_frame(GraphicsEngine *engine);
00126     void do_flip(GraphicsEngine *engine);
00127     void do_release(GraphicsEngine *engine);
00128     void do_close(GraphicsEngine *engine);
00129     void do_pending(GraphicsEngine *engine);
00130     bool any_done_gsgs() const;
00131 
00132     Windows _cull;    // cull stage
00133     Windows _cdraw;   // cull-and-draw-together stage
00134     Windows _draw;    // draw stage
00135     Windows _window;  // window stage, i.e. process windowing events 
00136     Windows _pending_release; // moved from _draw, pending release_gsg.
00137     Windows _pending_close;   // moved from _window, pending close.
00138     GSGs _gsgs;       // draw stage
00139     Mutex _wl_lock;
00140   };
00141 
00142   enum ThreadState {
00143     TS_wait,
00144     TS_do_frame,
00145     TS_do_flip,
00146     TS_do_release,
00147     TS_terminate
00148   };
00149 
00150   class RenderThread : public Thread, public WindowRenderer {
00151   public:
00152     RenderThread(const string &name, GraphicsEngine *engine);
00153     virtual void thread_main();
00154 
00155     GraphicsEngine *_engine;
00156     Mutex _cv_mutex;
00157     ConditionVar _cv;
00158     ThreadState _thread_state;
00159   };
00160 
00161   WindowRenderer *get_window_renderer(const string &name);
00162 
00163   Pipeline *_pipeline;
00164   Windows _windows;
00165 
00166   WindowRenderer _app;
00167   typedef pmap<string, PT(RenderThread) > Threads;
00168   Threads _threads;
00169   FrameBufferProperties _frame_buffer_properties;
00170   GraphicsThreadingModel _threading_model;
00171   bool _auto_flip;
00172 
00173   enum FlipState {
00174     FS_draw,  // Still drawing.
00175     FS_sync,  // All windows are done drawing.
00176     FS_flip,  // All windows are done drawing and have flipped.
00177   };
00178   FlipState _flip_state;
00179   Mutex _lock;
00180 
00181   static PStatCollector _cull_pcollector;
00182   static PStatCollector _draw_pcollector;
00183   friend class WindowRenderer;
00184 };
00185 
00186 #include "graphicsEngine.I"
00187 
00188 #endif
00189 

Generated on Fri May 2 00:36:26 2003 for Panda by doxygen1.3