graphicsOutput.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Filename: graphicsOutput.h
  2. // Created by: drose (06Feb04)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #ifndef GRAPHICSOUTPUT_H
  19. #define GRAPHICSOUTPUT_H
  20. #include "pandabase.h"
  21. #include "graphicsPipe.h"
  22. #include "displayRegion.h"
  23. #include "graphicsStateGuardian.h"
  24. #include "drawableRegion.h"
  25. #include "renderBuffer.h"
  26. #include "typedWritableReferenceCount.h"
  27. #include "pStatCollector.h"
  28. #include "notify.h"
  29. #include "pmutex.h"
  30. #include "filename.h"
  31. #include "drawMask.h"
  32. #include "pvector.h"
  33. #include "weakPointerTo.h"
  34. class PNMImage;
  35. ////////////////////////////////////////////////////////////////////
  36. // Class : GraphicsOutput
  37. // Description : This is a base class for the various different
  38. // classes that represent the result of a frame of
  39. // rendering. The most common kind of GraphicsOutput is
  40. // a GraphicsWindow, which is a real-time window on the
  41. // desktop, but another example is GraphicsBuffer, which
  42. // is an offscreen buffer.
  43. //
  44. // The actual rendering, and anything associated with
  45. // the graphics context itself, is managed by the
  46. // associated GraphicsStateGuardian (which might output
  47. // to multiple GraphicsOutput objects).
  48. //
  49. // GraphicsOutputs are not actually writable to bam
  50. // files, of course, but they may be passed as event
  51. // parameters, so they inherit from
  52. // TypedWritableReferenceCount instead of
  53. // TypedReferenceCount for that convenience.
  54. ////////////////////////////////////////////////////////////////////
  55. class EXPCL_PANDA GraphicsOutput : public TypedWritableReferenceCount, public DrawableRegion {
  56. protected:
  57. GraphicsOutput(GraphicsPipe *pipe, GraphicsStateGuardian *gsg,
  58. const string &name);
  59. private:
  60. GraphicsOutput(const GraphicsOutput &copy);
  61. void operator = (const GraphicsOutput &copy);
  62. PUBLISHED:
  63. virtual ~GraphicsOutput();
  64. INLINE GraphicsStateGuardian *get_gsg() const;
  65. INLINE GraphicsPipe *get_pipe() const;
  66. INLINE const string &get_name() const;
  67. INLINE bool has_texture() const;
  68. INLINE Texture *get_texture() const;
  69. void setup_render_texture(Texture *tex, bool allow_bind, bool to_ram);
  70. INLINE int get_x_size() const;
  71. INLINE int get_y_size() const;
  72. INLINE bool has_size() const;
  73. INLINE bool is_valid() const;
  74. void set_active(bool active);
  75. virtual bool is_active() const;
  76. INLINE void set_one_shot(bool one_shot);
  77. INLINE bool get_one_shot() const;
  78. void set_inverted(bool inverted);
  79. INLINE bool get_inverted() const;
  80. INLINE void clear_delete_flag();
  81. INLINE bool get_delete_flag() const;
  82. void set_sort(int sort);
  83. INLINE int get_sort() const;
  84. INLINE DisplayRegion *make_display_region();
  85. INLINE DisplayRegion *make_display_region(float l, float r,
  86. float b, float t);
  87. bool remove_display_region(DisplayRegion *display_region);
  88. void remove_all_display_regions();
  89. int get_num_display_regions() const;
  90. PT(DisplayRegion) get_display_region(int n) const;
  91. int get_num_active_display_regions() const;
  92. PT(DisplayRegion) get_active_display_region(int n) const;
  93. GraphicsOutput *make_texture_buffer(const string &name, int x_size, int y_size,
  94. Texture *tex = NULL, bool to_ram = false);
  95. GraphicsOutput *make_cube_map(const string &name, int size,
  96. NodePath &camera_rig,
  97. DrawMask camera_mask = DrawMask::all_on(),
  98. bool to_ram = false);
  99. INLINE static Filename make_screenshot_filename(const string &prefix = "screenshot");
  100. INLINE Filename save_screenshot_default(const string &prefix = "screenshot");
  101. INLINE bool save_screenshot(const Filename &filename);
  102. INLINE bool get_screenshot(PNMImage &image);
  103. public:
  104. // These are not intended to be called directly by the user.
  105. INLINE bool needs_context() const;
  106. INLINE bool flip_ready() const;
  107. INLINE bool operator < (const GraphicsOutput &other) const;
  108. virtual GraphicsOutput *get_host();
  109. virtual void request_open();
  110. virtual void request_close();
  111. virtual void set_close_now();
  112. virtual void reset_window(bool swapchain);
  113. // It is an error to call any of the following methods from any
  114. // thread other than the draw thread. These methods are normally
  115. // called by the GraphicsEngine.
  116. virtual bool begin_frame();
  117. void clear();
  118. virtual void end_frame();
  119. void change_scenes(DisplayRegion *new_dr);
  120. virtual void select_cube_map(int cube_map_index);
  121. // This method is called in the draw thread prior to issuing any
  122. // drawing commands for the window.
  123. virtual bool make_context();
  124. virtual void make_current();
  125. virtual void release_gsg();
  126. // These methods will be called within the app (main) thread.
  127. virtual void begin_flip();
  128. virtual void end_flip();
  129. // It is an error to call any of the following methods from any
  130. // thread other than the window thread. These methods are normally
  131. // called by the GraphicsEngine.
  132. virtual void process_events();
  133. protected:
  134. enum RenderTextureMode {
  135. RTM_none,
  136. RTM_bind_texture,
  137. RTM_bind_if_possible,
  138. RTM_copy_texture,
  139. RTM_copy_ram,
  140. };
  141. PT(GraphicsStateGuardian) _gsg;
  142. PT(GraphicsPipe) _pipe;
  143. string _name;
  144. PT(Texture) _texture;
  145. RenderTextureMode _rtm_mode;
  146. bool _flip_ready;
  147. bool _needs_context;
  148. int _cube_map_index;
  149. DisplayRegion *_cube_map_dr;
  150. private:
  151. DisplayRegion *add_display_region(DisplayRegion *display_region);
  152. INLINE void win_display_regions_changed();
  153. INLINE void determine_display_regions() const;
  154. void do_determine_display_regions();
  155. int _sort;
  156. unsigned int _internal_sort_index;
  157. protected:
  158. bool _active;
  159. bool _one_shot;
  160. bool _inverted;
  161. bool _delete_flag;
  162. // This weak pointer is used to keep track of whether the buffer's
  163. // bound Texture has been deleted or not. Until it has, we don't
  164. // auto-close the buffer (since that would deallocate the memory
  165. // associated with the texture).
  166. WPT(Texture) _hold_texture;
  167. protected:
  168. Mutex _lock;
  169. // protects _display_regions.
  170. PT(DisplayRegion) _default_display_region;
  171. typedef pvector< PT(DisplayRegion) > TotalDisplayRegions;
  172. TotalDisplayRegions _total_display_regions;
  173. typedef pvector<DisplayRegion *> ActiveDisplayRegions;
  174. ActiveDisplayRegions _active_display_regions;
  175. bool _display_regions_stale;
  176. protected:
  177. int _x_size;
  178. int _y_size;
  179. bool _has_size;
  180. bool _is_valid;
  181. static PStatCollector _make_current_pcollector;
  182. static PStatCollector _copy_texture_pcollector;
  183. public:
  184. static TypeHandle get_class_type() {
  185. return _type_handle;
  186. }
  187. static void init_type() {
  188. TypedWritableReferenceCount::init_type();
  189. register_type(_type_handle, "GraphicsOutput",
  190. TypedWritableReferenceCount::get_class_type());
  191. }
  192. virtual TypeHandle get_type() const {
  193. return get_class_type();
  194. }
  195. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  196. private:
  197. static TypeHandle _type_handle;
  198. friend class GraphicsPipe;
  199. friend class GraphicsEngine;
  200. friend class DisplayRegion;
  201. };
  202. #include "graphicsOutput.I"
  203. #endif /* GRAPHICSOUTPUT_H */