Panda3D
tinyGraphicsBuffer.cxx
1 // Filename: tinyGraphicsBuffer.cxx
2 // Created by: drose (08Aug08)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "pandabase.h"
16 
17 #include "tinyGraphicsBuffer.h"
18 #include "config_tinydisplay.h"
19 #include "tinyGraphicsStateGuardian.h"
20 #include "pStatTimer.h"
21 
22 TypeHandle TinyGraphicsBuffer::_type_handle;
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: TinyGraphicsBuffer::Constructor
26 // Access: Public
27 // Description:
28 ////////////////////////////////////////////////////////////////////
29 TinyGraphicsBuffer::
30 TinyGraphicsBuffer(GraphicsEngine *engine, GraphicsPipe *pipe,
31  const string &name,
32  const FrameBufferProperties &fb_prop,
33  const WindowProperties &win_prop,
34  int flags,
36  GraphicsOutput *host) :
37  GraphicsBuffer(engine, pipe, name, fb_prop, win_prop, flags, gsg, host)
38 {
39  _frame_buffer = NULL;
40 }
41 
42 ////////////////////////////////////////////////////////////////////
43 // Function: TinyGraphicsBuffer::Destructor
44 // Access: Public, Virtual
45 // Description:
46 ////////////////////////////////////////////////////////////////////
47 TinyGraphicsBuffer::
48 ~TinyGraphicsBuffer() {
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: TinyGraphicsBuffer::begin_frame
53 // Access: Public, Virtual
54 // Description: This function will be called within the draw thread
55 // before beginning rendering for a given frame. It
56 // should do whatever setup is required, and return true
57 // if the frame should be rendered, or false if it
58 // should be skipped.
59 ////////////////////////////////////////////////////////////////////
61 begin_frame(FrameMode mode, Thread *current_thread) {
62  begin_frame_spam(mode);
63  if (_gsg == (GraphicsStateGuardian *)NULL) {
64  return false;
65  }
66 
68  DCAST_INTO_R(tinygsg, _gsg, false);
69 
70  tinygsg->_current_frame_buffer = _frame_buffer;
71  tinygsg->reset_if_new();
72 
73  _gsg->set_current_properties(&get_fb_properties());
74  return _gsg->begin_frame(current_thread);
75 }
76 
77 ////////////////////////////////////////////////////////////////////
78 // Function: TinyGraphicsBuffer::end_frame
79 // Access: Public, Virtual
80 // Description: This function will be called within the draw thread
81 // after rendering is completed for a given frame. It
82 // should do whatever finalization is required.
83 ////////////////////////////////////////////////////////////////////
85 end_frame(FrameMode mode, Thread *current_thread) {
86  end_frame_spam(mode);
87  nassertv(_gsg != (GraphicsStateGuardian *)NULL);
88 
89  if (mode == FM_render) {
90  // end_render_texture();
91  copy_to_textures();
92  }
93 
94  _gsg->end_frame(current_thread);
95 
96  if (mode == FM_render) {
97  trigger_flip();
98  clear_cube_map_selection();
99  }
100 }
101 
102 ////////////////////////////////////////////////////////////////////
103 // Function: TinyGraphicsBuffer::close_buffer
104 // Access: Protected, Virtual
105 // Description: Closes the buffer right now. Called from the buffer
106 // thread.
107 ////////////////////////////////////////////////////////////////////
108 void TinyGraphicsBuffer::
109 close_buffer() {
110  if (_gsg != (GraphicsStateGuardian *)NULL) {
111  TinyGraphicsStateGuardian *tinygsg;
112  DCAST_INTO_V(tinygsg, _gsg);
113  tinygsg->_current_frame_buffer = NULL;
114  _gsg.clear();
115  }
116 
117  _is_valid = false;
118 }
119 
120 ////////////////////////////////////////////////////////////////////
121 // Function: TinyGraphicsBuffer::open_buffer
122 // Access: Protected, Virtual
123 // Description: Opens the buffer right now. Called from the buffer
124 // thread. Returns true if the buffer is successfully
125 // opened, or false if there was a problem.
126 ////////////////////////////////////////////////////////////////////
127 bool TinyGraphicsBuffer::
128 open_buffer() {
129  // GSG Creation/Initialization
130  TinyGraphicsStateGuardian *tinygsg;
131  if (_gsg == 0) {
132  // There is no old gsg. Create a new one.
133  tinygsg = new TinyGraphicsStateGuardian(_engine, _pipe, NULL);
134  _gsg = tinygsg;
135  } else {
136  DCAST_INTO_R(tinygsg, _gsg, false);
137  }
138 
139  create_frame_buffer();
140  if (_frame_buffer == NULL) {
141  tinydisplay_cat.error()
142  << "Could not create frame buffer.\n";
143  return false;
144  }
145 
146  tinygsg->_current_frame_buffer = _frame_buffer;
147 
148  tinygsg->reset_if_new();
149  if (!tinygsg->is_valid()) {
150  close_buffer();
151  return false;
152  }
153 
154  _is_valid = true;
155  return true;
156 }
157 
158 ////////////////////////////////////////////////////////////////////
159 // Function: TinyGraphicsBuffer::create_frame_buffer
160 // Access: Private
161 // Description: Creates a suitable frame buffer for the current
162 // window size.
163 ////////////////////////////////////////////////////////////////////
164 void TinyGraphicsBuffer::
165 create_frame_buffer() {
166  if (_frame_buffer != NULL) {
167  ZB_close(_frame_buffer);
168  _frame_buffer = NULL;
169  }
170 
171  _frame_buffer = ZB_open(get_fb_x_size(), get_fb_y_size(), ZB_MODE_RGBA, 0, 0, 0, 0);
172 }
173 
virtual void clear(DrawableRegion *clearable)
Clears the framebuffer within the current DisplayRegion, according to the flags indicated by the give...
virtual bool begin_frame(FrameMode mode, Thread *current_thread)
This function will be called within the draw thread before beginning rendering for a given frame...
bool reset_if_new()
Calls reset() to initialize the GSG, but only if it hasn&#39;t been called yet.
int get_fb_y_size() const
Returns the internal height of the window or buffer.
A container for the various kinds of properties we might ask to have on a graphics window before we o...
An offscreen buffer for rendering into.
An object to create GraphicsOutputs that share a particular 3-D API.
Definition: graphicsPipe.h:58
This is a base class for the various different classes that represent the result of a frame of render...
int get_fb_x_size() const
Returns the internal width of the window or buffer.
A thread; that is, a lightweight process.
Definition: thread.h:51
Encapsulates all the communication with a particular instance of a given rendering backend...
bool is_valid() const
Returns true if the GSG has been correctly initialized within a graphics context, false if there has ...
This class is the main interface to controlling the render process.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
A container for the various kinds of properties we might ask to have on a graphics frameBuffer before...
virtual void end_frame(FrameMode mode, Thread *current_thread)
This function will be called within the draw thread after rendering is completed for a given frame...
An interface to the TinyPanda software rendering code within this module.
const FrameBufferProperties & get_fb_properties() const
Returns the framebuffer properties of the window.