Panda3D
parasiteBuffer.cxx
1 // Filename: parasiteBuffer.cxx
2 // Created by: drose (27Feb04)
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 "parasiteBuffer.h"
16 #include "texture.h"
17 
18 TypeHandle ParasiteBuffer::_type_handle;
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: ParasiteBuffer::Constructor
22 // Access: Public
23 // Description: Normally, the ParasiteBuffer constructor is not
24 // called directly; these are created instead via the
25 // GraphicsEngine::make_parasite() function.
26 ////////////////////////////////////////////////////////////////////
28 ParasiteBuffer(GraphicsOutput *host, const string &name,
29  int x_size, int y_size, int flags) :
30  GraphicsOutput(host->get_engine(), host->get_pipe(),
31  name, host->get_fb_properties(),
32  WindowProperties::size(x_size, y_size), flags,
33  host->get_gsg(), host, false)
34 {
35 #ifdef DO_MEMORY_USAGE
36  MemoryUsage::update_type(this, this);
37 #endif
38 
39  if (display_cat.is_debug()) {
40  display_cat.debug()
41  << "Creating new parasite buffer " << get_name()
42  << " on " << _host->get_name() << "\n";
43  }
44 
45  _creation_flags = flags;
46 
47  if (flags & GraphicsPipe::BF_size_track_host) {
48  _size = host->get_size();
49  } else {
50  _size.set(x_size, y_size);
51  }
52 
53  _has_size = true;
54  _overlay_display_region->compute_pixels(_size.get_x(), _size.get_y());
55  _is_valid = true;
56 
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: ParasiteBuffer::Destructor
62 // Access: Published, Virtual
63 // Description:
64 ////////////////////////////////////////////////////////////////////
65 ParasiteBuffer::
66 ~ParasiteBuffer() {
67  _is_valid = false;
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: ParasiteBuffer::is_active
72 // Access: Published, Virtual
73 // Description: Returns true if the window is ready to be rendered
74 // into, false otherwise.
75 ////////////////////////////////////////////////////////////////////
77 is_active() const {
78  return GraphicsOutput::is_active() && _host->is_active();
79 }
80 
81 ////////////////////////////////////////////////////////////////////
82 // Function: ParasiteBuffer::set_size
83 // Access: Public, Virtual
84 // Description: This is called by the GraphicsEngine to request that
85 // the buffer resize itself. Although calls to get the
86 // size will return the new value, much of the actual
87 // resizing work doesn't take place until the next
88 // begin_frame. Not all buffers are resizeable.
89 ////////////////////////////////////////////////////////////////////
91 set_size(int x, int y) {
92  if ((_creation_flags & GraphicsPipe::BF_resizeable) == 0) {
93  nassert_raise("Cannot resize buffer unless it is created with BF_resizeable flag");
94  return;
95  }
96  set_size_and_recalc(x, y);
97 }
98 
99 ////////////////////////////////////////////////////////////////////
100 // Function: ParasiteBuffer::set_size_and_recalc
101 // Access: Public
102 // Description:
103 ////////////////////////////////////////////////////////////////////
104 void ParasiteBuffer::
105 set_size_and_recalc(int x, int y) {
106  if (!(_creation_flags & GraphicsPipe::BF_size_track_host)) {
107  if (_creation_flags & GraphicsPipe::BF_size_power_2) {
110  }
111  if (_creation_flags & GraphicsPipe::BF_size_square) {
112  x = y = min(x, y);
113  }
114  }
115 
117 }
118 
119 ////////////////////////////////////////////////////////////////////
120 // Function: ParasiteBuffer::flip_ready
121 // Access: Public, Virtual
122 // Description: Returns true if a frame has been rendered and needs
123 // to be flipped, false otherwise.
124 ////////////////////////////////////////////////////////////////////
125 bool ParasiteBuffer::
126 flip_ready() const {
127  nassertr(_host != NULL, false);
128  return _host->flip_ready();
129 }
130 
131 ////////////////////////////////////////////////////////////////////
132 // Function: ParasiteBuffer::begin_flip
133 // Access: Public, Virtual
134 // Description: This function will be called within the draw thread
135 // after end_frame() has been called on all windows, to
136 // initiate the exchange of the front and back buffers.
137 //
138 // This should instruct the window to prepare for the
139 // flip at the next video sync, but it should not wait.
140 //
141 // We have the two separate functions, begin_flip() and
142 // end_flip(), to make it easier to flip all of the
143 // windows at the same time.
144 ////////////////////////////////////////////////////////////////////
145 void ParasiteBuffer::
147  nassertv(_host != NULL);
148  _host->begin_flip();
149 }
150 
151 ////////////////////////////////////////////////////////////////////
152 // Function: ParasiteBuffer::ready_flip
153 // Access: Public, Virtual
154 // Description: This function will be called within the draw thread
155 // after end_frame() has been called on all windows, to
156 // initiate the exchange of the front and back buffers.
157 //
158 // This should instruct the window to prepare for the
159 // flip when it is command but not actually flip
160 //
161 ////////////////////////////////////////////////////////////////////
162 void ParasiteBuffer::
164  nassertv(_host != NULL);
165  _host->ready_flip();
166 }
167 
168 ////////////////////////////////////////////////////////////////////
169 // Function: ParasiteBuffer::end_flip
170 // Access: Public, Virtual
171 // Description: This function will be called within the draw thread
172 // after begin_flip() has been called on all windows, to
173 // finish the exchange of the front and back buffers.
174 //
175 // This should cause the window to wait for the flip, if
176 // necessary.
177 ////////////////////////////////////////////////////////////////////
178 void ParasiteBuffer::
180  nassertv(_host != NULL);
181  _host->end_flip();
182  _flip_ready = false;
183 }
184 
185 ////////////////////////////////////////////////////////////////////
186 // Function: ParasiteBuffer::begin_frame
187 // Access: Public, Virtual
188 // Description: This function will be called within the draw thread
189 // before beginning rendering for a given frame. It
190 // should do whatever setup is required, and return true
191 // if the frame should be rendered, or false if it
192 // should be skipped.
193 ////////////////////////////////////////////////////////////////////
194 bool ParasiteBuffer::
195 begin_frame(FrameMode mode, Thread *current_thread) {
196  begin_frame_spam(mode);
197 
198  if (!_host->begin_frame(FM_parasite, current_thread)) {
199  return false;
200  }
201 
202  if (_creation_flags & GraphicsPipe::BF_size_track_host) {
203  if (_host->get_size() != _size) {
204  set_size_and_recalc(_host->get_x_size(),
205  _host->get_y_size());
206  }
207  } else {
208  if (_host->get_x_size() < get_x_size() ||
209  _host->get_y_size() < get_y_size()) {
210  set_size_and_recalc(min(get_x_size(), _host->get_x_size()),
211  min(get_y_size(), _host->get_y_size()));
212  }
213  }
214 
215  clear_cube_map_selection();
216  return true;
217 }
218 
219 ////////////////////////////////////////////////////////////////////
220 // Function: ParasiteBuffer::end_frame
221 // Access: Public, Virtual
222 // Description: This function will be called within the draw thread
223 // after rendering is completed for a given frame. It
224 // should do whatever finalization is required.
225 ////////////////////////////////////////////////////////////////////
226 void ParasiteBuffer::
227 end_frame(FrameMode mode, Thread *current_thread) {
228  end_frame_spam(mode);
229 
230  nassertv(_gsg != (GraphicsStateGuardian *)NULL);
231 
232  _host->end_frame(FM_parasite, current_thread);
233 
234  if (mode == FM_refresh) {
235  return;
236  }
237 
238  if (mode == FM_render) {
239  promote_to_copy_texture();
240  copy_to_textures();
241  clear_cube_map_selection();
242  }
243 }
244 
245 ////////////////////////////////////////////////////////////////////
246 // Function: ParasiteBuffer::get_host
247 // Access: Public, Virtual
248 // Description: This is normally called only from within
249 // make_texture_buffer(). When called on a
250 // ParasiteBuffer, it returns the host of that buffer;
251 // but when called on some other buffer, it returns the
252 // buffer itself.
253 ////////////////////////////////////////////////////////////////////
256  return _host;
257 }
258 
virtual void begin_flip()
This function will be called within the draw thread after end_frame() has been called on all windows...
void set_size_and_recalc(int x, int y)
Changes the x_size and y_size, then recalculates structures that depend on size.
const LVecBase2i & get_size() const
Returns the visible size of the window or buffer, if it is known.
virtual bool is_active() const
Returns true if the window is ready to be rendered into, false otherwise.
virtual GraphicsOutput * get_host()
This is normally called only from within make_texture_buffer().
int get_y_size() const
Returns the visible height of the window or buffer, if it is known.
virtual bool flip_ready() const
Returns true if a frame has been rendered and needs to be flipped, false otherwise.
virtual bool is_active() const
Returns true if the window is ready to be rendered into, false otherwise.
virtual void end_flip()
This function will be called within the draw thread after begin_flip() has been called on all windows...
A container for the various kinds of properties we might ask to have on a graphics window before we o...
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...
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...
void set_inverted(bool inverted)
Changes the current setting of the inverted flag.
virtual void ready_flip()
This function will be called within the draw thread after end_frame() has been called on all windows...
This is a base class for the various different classes that represent the result of a frame of render...
int get_x_size() const
Returns the visible width of the window or buffer, if it is known.
ParasiteBuffer(GraphicsOutput *host, const string &name, int x_size, int y_size, int flags)
Normally, the ParasiteBuffer constructor is not called directly; these are created instead via the Gr...
GraphicsStateGuardian * get_gsg() const
Returns the GSG that is associated with this window.
void set_size(int x, int y)
This is called by the GraphicsEngine to request that the buffer resize itself.
A thread; that is, a lightweight process.
Definition: thread.h:51
Encapsulates all the communication with a particular instance of a given rendering backend...
const string & get_name() const
Returns the name that was passed to the GraphicsOutput constructor.
static int down_to_power_2(int value)
Returns the largest power of 2 less than or equal to value.
Definition: texture.cxx:1860
bool get_copy_texture_inverted() const
Returns true if this particular GSG has the property that any framebuffer-to-texture copy results in ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85