Panda3D
parasiteBuffer.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file parasiteBuffer.cxx
10  * @author drose
11  * @date 2004-02-27
12  */
13 
14 #include "parasiteBuffer.h"
15 #include "texture.h"
16 
17 TypeHandle ParasiteBuffer::_type_handle;
18 
19 /**
20  * Normally, the ParasiteBuffer constructor is not called directly; these are
21  * created instead via the GraphicsEngine::make_parasite() function.
22  */
24 ParasiteBuffer(GraphicsOutput *host, const std::string &name,
25  int x_size, int y_size, int flags) :
26  GraphicsOutput(host->get_engine(), host->get_pipe(),
27  name, host->get_fb_properties(),
28  WindowProperties::size(x_size, y_size), flags,
29  host->get_gsg(), host, false)
30 {
31 #ifdef DO_MEMORY_USAGE
32  MemoryUsage::update_type(this, this);
33 #endif
34 
35  if (display_cat.is_debug()) {
36  display_cat.debug()
37  << "Creating new parasite buffer " << get_name()
38  << " on " << _host->get_name() << "\n";
39  }
40 
41  _creation_flags = flags;
42 
43  if (flags & GraphicsPipe::BF_size_track_host) {
44  _size = host->get_size();
45  } else {
46  _size.set(x_size, y_size);
47  }
48 
49  _has_size = true;
50  _overlay_display_region->compute_pixels(_size.get_x(), _size.get_y());
51  _is_valid = true;
52 
53  set_inverted(host->get_gsg()->get_copy_texture_inverted());
54 }
55 
56 /**
57  *
58  */
59 ParasiteBuffer::
60 ~ParasiteBuffer() {
61  _is_valid = false;
62 }
63 
64 /**
65  * Returns true if the window is ready to be rendered into, false otherwise.
66  */
68 is_active() const {
69  return GraphicsOutput::is_active() && _host->is_active();
70 }
71 
72 /**
73  * This is called by the GraphicsEngine to request that the buffer resize
74  * itself. Although calls to get the size will return the new value, much of
75  * the actual resizing work doesn't take place until the next begin_frame.
76  * Not all buffers are resizeable.
77  */
79 set_size(int x, int y) {
80  if ((_creation_flags & GraphicsPipe::BF_resizeable) == 0) {
81  nassert_raise("Cannot resize buffer unless it is created with BF_resizeable flag");
82  return;
83  }
84  set_size_and_recalc(x, y);
85 }
86 
87 /**
88  *
89  */
90 void ParasiteBuffer::
91 set_size_and_recalc(int x, int y) {
92  if (!(_creation_flags & GraphicsPipe::BF_size_track_host)) {
93  if (_creation_flags & GraphicsPipe::BF_size_power_2) {
96  }
97  if (_creation_flags & GraphicsPipe::BF_size_square) {
98  x = y = std::min(x, y);
99  }
100  }
101 
103 }
104 
105 /**
106  * Returns true if a frame has been rendered and needs to be flipped, false
107  * otherwise.
108  */
109 bool ParasiteBuffer::
110 flip_ready() const {
111  nassertr(_host != nullptr, false);
112  return _host->flip_ready();
113 }
114 
115 /**
116  * This function will be called within the draw thread after end_frame() has
117  * been called on all windows, to initiate the exchange of the front and back
118  * buffers.
119  *
120  * This should instruct the window to prepare for the flip at the next video
121  * sync, but it should not wait.
122  *
123  * We have the two separate functions, begin_flip() and end_flip(), to make it
124  * easier to flip all of the windows at the same time.
125  */
126 void ParasiteBuffer::
128  nassertv(_host != nullptr);
129  _host->begin_flip();
130 }
131 
132 /**
133  * This function will be called within the draw thread after end_frame() has
134  * been called on all windows, to initiate the exchange of the front and back
135  * buffers.
136  *
137  * This should instruct the window to prepare for the flip when it is command
138  * but not actually flip
139  *
140  */
141 void ParasiteBuffer::
143  nassertv(_host != nullptr);
144  _host->ready_flip();
145 }
146 
147 /**
148  * This function will be called within the draw thread after begin_flip() has
149  * been called on all windows, to finish the exchange of the front and back
150  * buffers.
151  *
152  * This should cause the window to wait for the flip, if necessary.
153  */
154 void ParasiteBuffer::
156  nassertv(_host != nullptr);
157  _host->end_flip();
158  _flip_ready = false;
159 }
160 
161 /**
162  * This function will be called within the draw thread before beginning
163  * rendering for a given frame. It should do whatever setup is required, and
164  * return true if the frame should be rendered, or false if it should be
165  * skipped.
166  */
167 bool ParasiteBuffer::
168 begin_frame(FrameMode mode, Thread *current_thread) {
169  begin_frame_spam(mode);
170 
171  if (!_host->begin_frame(FM_parasite, current_thread)) {
172  return false;
173  }
174 
175  if (_creation_flags & GraphicsPipe::BF_size_track_host) {
176  if (_host->get_size() != _size) {
177  set_size_and_recalc(_host->get_x_size(),
178  _host->get_y_size());
179  }
180  } else {
181  if (_host->get_x_size() < get_x_size() ||
182  _host->get_y_size() < get_y_size()) {
183  set_size_and_recalc(std::min(get_x_size(), _host->get_x_size()),
184  std::min(get_y_size(), _host->get_y_size()));
185  }
186  }
187 
188  clear_cube_map_selection();
189  return true;
190 }
191 
192 /**
193  * This function will be called within the draw thread after rendering is
194  * completed for a given frame. It should do whatever finalization is
195  * required.
196  */
197 void ParasiteBuffer::
198 end_frame(FrameMode mode, Thread *current_thread) {
199  end_frame_spam(mode);
200 
201  nassertv(_gsg != nullptr);
202 
203  _host->end_frame(FM_parasite, current_thread);
204 
205  if (mode == FM_refresh) {
206  return;
207  }
208 
209  if (mode == FM_render) {
210  promote_to_copy_texture();
211  copy_to_textures();
212  clear_cube_map_selection();
213  }
214 }
215 
216 /**
217  * This is normally called only from within make_texture_buffer(). When
218  * called on a ParasiteBuffer, it returns the host of that buffer; but when
219  * called on some other buffer, it returns the buffer itself.
220  */
223  return _host;
224 }
ParasiteBuffer(GraphicsOutput *host, const std::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...
virtual void begin_flip()
This function will be called within the draw thread after end_frame() has been called on all windows,...
get_size
Returns the visible size of the window or buffer, if it is known.
void set_size_and_recalc(int x, int y)
Changes the x_size and y_size, then recalculates structures that depend on size.
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().
is_active
Returns true if the window is ready to be rendered into, false otherwise.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
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.
static void update_type(ReferenceCount *ptr, TypeHandle type)
Associates the indicated type with the given pointer.
Definition: memoryUsage.I:55
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
get_name
Returns the name that was passed to the GraphicsOutput constructor.
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:46
static int down_to_power_2(int value)
Returns the largest power of 2 less than or equal to value.
Definition: texture.cxx:1995
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
set_inverted
Changes the current setting of the inverted flag.
get_gsg
Returns the GSG that is associated with this window.