Panda3D
callbackGraphicsWindow.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 callbackGraphicsWindow.cxx
10  * @author drose
11  * @date 2011-01-06
12  */
13 
14 #include "callbackGraphicsWindow.h"
15 
16 TypeHandle CallbackGraphicsWindow::_type_handle;
17 TypeHandle CallbackGraphicsWindow::WindowCallbackData::_type_handle;
18 TypeHandle CallbackGraphicsWindow::EventsCallbackData::_type_handle;
19 TypeHandle CallbackGraphicsWindow::PropertiesCallbackData::_type_handle;
20 TypeHandle CallbackGraphicsWindow::RenderCallbackData::_type_handle;
21 
22 /**
23  * Use GraphicsEngine::make_output() to construct a CallbackGraphicsWindow.
24  */
25 CallbackGraphicsWindow::
26 CallbackGraphicsWindow(GraphicsEngine *engine, GraphicsPipe *pipe,
27  const std::string &name,
28  const FrameBufferProperties &fb_prop,
29  const WindowProperties &win_prop,
30  int flags,
31  GraphicsStateGuardian *gsg) :
32  GraphicsWindow(engine, pipe, name, fb_prop, win_prop, flags, gsg, nullptr)
33 {
34 #ifdef DO_MEMORY_USAGE
35  MemoryUsage::update_type(this, this);
36 #endif
37 
38  // Let's ensure that these properties are set to *something* initially.
39  _properties.set_origin(0, 0);
40  _properties.set_size(0, 0);
41 }
42 
43 /**
44  *
45  */
46 CallbackGraphicsWindow::
47 ~CallbackGraphicsWindow() {
48 }
49 
50 /**
51  * Adds a new input device (mouse) to the window with the indicated name.
52  * Returns the index of the new device.
53  */
55 create_input_device(const std::string &name) {
56  return add_input_device(GraphicsWindowInputDevice::pointer_and_keyboard(this, name));
57 }
58 
59 /**
60  * This function will be called within the draw thread before beginning
61  * rendering for a given frame. It should do whatever setup is required, and
62  * return true if the frame should be rendered, or false if it should be
63  * skipped.
64  */
66 begin_frame(FrameMode mode, Thread *current_thread) {
67  bool result = false;
68  if (_render_callback != nullptr) {
69  RenderCallbackData data(this, RCT_begin_frame, mode);
70  _render_callback->do_callback(&data);
71  result = data.get_render_flag();
72  } else {
73  result = GraphicsWindow::begin_frame(mode, current_thread);
74  }
75 
76  if (!result) {
77  return false;
78  }
79 
80  _gsg->reset_if_new();
81  _gsg->set_current_properties(&get_fb_properties());
82 
83  return _gsg->begin_frame(current_thread);
84 }
85 
86 /**
87  * This function will be called within the draw thread after rendering is
88  * completed for a given frame. It should do whatever finalization is
89  * required.
90  */
92 end_frame(FrameMode mode, Thread *current_thread) {
93  if (_render_callback != nullptr) {
94  // In case the callback or the application hosting the OpenGL context
95  // wants to do more rendering, let's give it a blank slate.
96  _gsg->set_state_and_transform(RenderState::make_empty(), _gsg->get_internal_transform());
97  _gsg->clear_before_callback();
98 
99  RenderCallbackData data(this, RCT_end_frame, mode);
100  _render_callback->do_callback(&data);
101  } else {
102  GraphicsWindow::end_frame(mode, current_thread);
103  }
104 
105  _gsg->end_frame(current_thread);
106 
107  if (mode == FM_render) {
108  trigger_flip();
109  clear_cube_map_selection();
110  }
111 }
112 
113 /**
114  * This function will be called within the draw thread after end_frame() has
115  * been called on all windows, to initiate the exchange of the front and back
116  * buffers.
117  *
118  * This should instruct the window to prepare for the flip at the next video
119  * sync, but it should not wait.
120  *
121  * We have the two separate functions, begin_flip() and end_flip(), to make it
122  * easier to flip all of the windows at the same time.
123  */
126  if (_render_callback != nullptr) {
127  RenderCallbackData data(this, RCT_begin_flip, FM_render);
128  _render_callback->do_callback(&data);
129  } else {
131  }
132 }
133 
134 /**
135  * This function will be called within the draw thread after begin_flip() has
136  * been called on all windows, to finish the exchange of the front and back
137  * buffers.
138  *
139  * This should cause the window to wait for the flip, if necessary.
140  */
143  if (_render_callback != nullptr) {
144  RenderCallbackData data(this, RCT_end_flip, FM_render);
145  _render_callback->do_callback(&data);
146  } else {
148  }
149 }
150 
151 /**
152  * Do whatever processing is necessary to ensure that the window responds to
153  * user events. Also, honor any requests recently made via
154  * request_properties().
155  *
156  * This function is called only within the window thread.
157  */
160  if (_events_callback != nullptr) {
161  EventsCallbackData data(this);
162  _events_callback->do_callback(&data);
163  } else {
165  }
166 }
167 
168 /**
169  * Applies the requested set of properties to the window, if possible, for
170  * instance to request a change in size or minimization status.
171  */
174  if (_properties_callback != nullptr) {
175  PropertiesCallbackData data(this, properties);
176  _properties_callback->do_callback(&data);
177  } else {
179  }
180 }
181 
182 /**
183  * Opens the window right now. Called from the window thread. Returns true
184  * if the window is successfully opened, or false if there was a problem.
185  */
186 bool CallbackGraphicsWindow::
187 open_window() {
188  // In this case, we assume the callback has handled the window opening.
189 
190  // We also assume the callback has given us an accurate
191  // FramebufferProperties, but we do go ahead and assume some certain minimum
192  // properties.
193  _fb_properties.set_rgb_color(1);
194 
195  if (_fb_properties.get_color_bits() == 0) {
196  _fb_properties.set_color_bits(16);
197  }
198  return true;
199 }
200 
201 /**
202  * Called from the window thread in response to a request from within the code
203  * (via request_properties()) to change the size and/or position of the
204  * window. Returns true if the window is successfully changed, or false if
205  * there was a problem.
206  */
207 bool CallbackGraphicsWindow::
208 do_reshape_request(int x_origin, int y_origin, bool has_origin,
209  int x_size, int y_size) {
210  // In this case, we assume the callback has handled the window resizing.
211  WindowProperties properties;
212  if (has_origin) {
213  properties.set_origin(x_origin, y_origin);
214  }
215  properties.set_size(x_size, y_size);
216  system_changed_properties(properties);
217 
218  return true;
219 }
220 
221 /**
222  *
223  */
226  _window->GraphicsWindow::process_events();
227 }
228 
229 /**
230  *
231  */
234  _window->GraphicsWindow::set_properties_now(_properties);
235 }
236 
237 /**
238  *
239  */
242  switch (_callback_type) {
243  case RCT_begin_frame:
244  {
245  bool render_flag = _window->GraphicsWindow::begin_frame(_frame_mode, Thread::get_current_thread());
246  set_render_flag(render_flag);
247  }
248  break;
249 
250  case RCT_end_frame:
251  _window->GraphicsWindow::end_frame(_frame_mode, Thread::get_current_thread());
252  break;
253 
254  case RCT_begin_flip:
255  _window->GraphicsWindow::begin_flip();
256  break;
257 
258  case RCT_end_flip:
259  _window->GraphicsWindow::end_flip();
260  break;
261  }
262 }
virtual void process_events()
Do whatever processing is necessary to ensure that the window responds to user events.
set_size
Specifies the requested size of the window, in pixels.
virtual void end_flip()
This function will be called within the draw thread after begin_flip() has been called on all windows...
virtual void set_properties_now(WindowProperties &properties)
Applies the requested set of properties to the window, if possible, for instance to request a change ...
virtual void begin_flip()
This function will be called within the draw thread after end_frame() has been called on all windows,...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
A window, fullscreen or on a desktop, into which a graphics device sends its output for interactive d...
A container for the various kinds of properties we might ask to have on a graphics window before we o...
virtual void process_events()
Do whatever processing is necessary to ensure that the window responds to user events.
set_origin
Specifies the origin on the screen (in pixels, relative to the top-left corner) at which the window s...
An object to create GraphicsOutputs that share a particular 3-D API.
Definition: graphicsPipe.h:52
virtual void upcall()
You should make this call during the callback if you want to continue the normal function that would ...
virtual void upcall()
You should make this call during the callback if you want to continue the normal function that would ...
int create_input_device(const std::string &name)
Adds a new input device (mouse) to the window with the indicated name.
static void update_type(ReferenceCount *ptr, TypeHandle type)
Associates the indicated type with the given pointer.
Definition: memoryUsage.I:55
virtual void set_properties_now(WindowProperties &properties)
Applies the requested set of properties to the window, if possible, for instance to request a change ...
A thread; that is, a lightweight process.
Definition: thread.h:46
virtual void begin_flip()
This function will be called within the draw thread after end_frame() has been called on all windows,...
Encapsulates all the communication with a particular instance of a given rendering backend.
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 end_frame(FrameMode mode, Thread *current_thread)
This function will be called within the draw thread after rendering is completed for a given frame.
This class is the main interface to controlling the render process.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
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 frameBuffer before...
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 upcall()
You should make this call during the callback if you want to continue the normal function that would ...
const FrameBufferProperties & get_fb_properties() const
Returns the framebuffer properties of the window.