tinyWinGraphicsWindow.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 tinyWinGraphicsWindow.cxx
  10. * @author drose
  11. * @date 2008-05-06
  12. */
  13. #include "pandabase.h"
  14. #ifdef _WIN32
  15. #include "tinyWinGraphicsWindow.h"
  16. #include "config_tinydisplay.h"
  17. #include "config_windisplay.h"
  18. #include "tinyWinGraphicsPipe.h"
  19. #include "tinyGraphicsStateGuardian.h"
  20. #include "pStatTimer.h"
  21. #include <wingdi.h>
  22. TypeHandle TinyWinGraphicsWindow::_type_handle;
  23. /**
  24. *
  25. */
  26. TinyWinGraphicsWindow::
  27. TinyWinGraphicsWindow(GraphicsEngine *engine, GraphicsPipe *pipe,
  28. const std::string &name,
  29. const FrameBufferProperties &fb_prop,
  30. const WindowProperties &win_prop,
  31. int flags,
  32. GraphicsStateGuardian *gsg,
  33. GraphicsOutput *host) :
  34. WinGraphicsWindow(engine, pipe, name, fb_prop, win_prop, flags, gsg, host)
  35. {
  36. _frame_buffer = nullptr;
  37. _hdc = (HDC)0;
  38. update_pixel_factor();
  39. }
  40. /**
  41. *
  42. */
  43. TinyWinGraphicsWindow::
  44. ~TinyWinGraphicsWindow() {
  45. }
  46. /**
  47. * This function will be called within the draw thread before beginning
  48. * rendering for a given frame. It should do whatever setup is required, and
  49. * return true if the frame should be rendered, or false if it should be
  50. * skipped.
  51. */
  52. bool TinyWinGraphicsWindow::
  53. begin_frame(FrameMode mode, Thread *current_thread) {
  54. begin_frame_spam(mode);
  55. if (_gsg == nullptr) {
  56. return false;
  57. }
  58. if (!get_unexposed_draw() && !_got_expose_event) {
  59. if (tinydisplay_cat.is_spam()) {
  60. tinydisplay_cat.spam()
  61. << "Not drawing " << this << ": unexposed.\n";
  62. }
  63. return false;
  64. }
  65. TinyGraphicsStateGuardian *tinygsg;
  66. DCAST_INTO_R(tinygsg, _gsg, false);
  67. tinygsg->_current_frame_buffer = _frame_buffer;
  68. tinygsg->reset_if_new();
  69. _gsg->set_current_properties(&get_fb_properties());
  70. if (_gsg->begin_frame(current_thread)) {
  71. copy_async_screenshot();
  72. return true;
  73. } else {
  74. return false;
  75. }
  76. }
  77. /**
  78. * This function will be called within the draw thread after rendering is
  79. * completed for a given frame. It should do whatever finalization is
  80. * required.
  81. */
  82. void TinyWinGraphicsWindow::
  83. end_frame(FrameMode mode, Thread *current_thread) {
  84. end_frame_spam(mode);
  85. nassertv(_gsg != nullptr);
  86. if (mode == FM_render) {
  87. // end_render_texture();
  88. copy_to_textures();
  89. }
  90. _gsg->end_frame(current_thread);
  91. if (mode == FM_render) {
  92. trigger_flip();
  93. clear_cube_map_selection();
  94. }
  95. }
  96. /**
  97. * This function will be called within the draw thread after begin_flip() has
  98. * been called on all windows, to finish the exchange of the front and back
  99. * buffers.
  100. *
  101. * This should cause the window to wait for the flip, if necessary.
  102. */
  103. void TinyWinGraphicsWindow::
  104. end_flip() {
  105. if (!_flip_ready) {
  106. GraphicsWindow::end_flip();
  107. return;
  108. }
  109. HBITMAP bm = CreateCompatibleBitmap(_hdc, _frame_buffer->xsize, _frame_buffer->ysize);
  110. HDC bmdc = CreateCompatibleDC(_hdc);
  111. SelectObject(bmdc, bm);
  112. int fb_xsize = get_fb_x_size();
  113. int fb_ysize = get_fb_y_size();
  114. int fb_ytop = _frame_buffer->ysize - fb_ysize;
  115. SetDIBits(_hdc, bm, fb_ytop, fb_ysize, _frame_buffer->pbuf,
  116. &_bitmap_info, DIB_RGB_COLORS);
  117. if (fb_xsize == _frame_buffer->xsize) {
  118. BitBlt(_hdc, 0, 0, fb_xsize, fb_ysize,
  119. bmdc, 0, 0, SRCCOPY);
  120. } else {
  121. // SetStretchBltMode(_hdc, HALFTONE);
  122. StretchBlt(_hdc, 0, 0, _frame_buffer->xsize, _frame_buffer->ysize,
  123. bmdc, 0, 0,fb_xsize, fb_ysize,
  124. SRCCOPY);
  125. }
  126. DeleteDC(bmdc);
  127. DeleteObject(bm);
  128. GdiFlush();
  129. GraphicsWindow::end_flip();
  130. }
  131. /**
  132. * Returns true if a call to set_pixel_zoom() will be respected, false if it
  133. * will be ignored. If this returns false, then get_pixel_factor() will
  134. * always return 1.0, regardless of what value you specify for
  135. * set_pixel_zoom().
  136. *
  137. * This may return false if the underlying renderer doesn't support pixel
  138. * zooming, or if you have called this on a DisplayRegion that doesn't have
  139. * both set_clear_color() and set_clear_depth() enabled.
  140. */
  141. bool TinyWinGraphicsWindow::
  142. supports_pixel_zoom() const {
  143. return true;
  144. }
  145. /**
  146. * Closes the window right now. Called from the window thread.
  147. */
  148. void TinyWinGraphicsWindow::
  149. close_window() {
  150. if (_gsg != nullptr) {
  151. TinyGraphicsStateGuardian *tinygsg;
  152. DCAST_INTO_V(tinygsg, _gsg);
  153. tinygsg->_current_frame_buffer = nullptr;
  154. _gsg.clear();
  155. }
  156. ReleaseDC(_hWnd, _hdc);
  157. _hdc = (HDC)0;
  158. WinGraphicsWindow::close_window();
  159. }
  160. /**
  161. * Opens the window right now. Called from the window thread. Returns true
  162. * if the window is successfully opened, or false if there was a problem.
  163. */
  164. bool TinyWinGraphicsWindow::
  165. open_window() {
  166. if (!WinGraphicsWindow::open_window()) {
  167. return false;
  168. }
  169. // GSG CreationInitialization
  170. TinyGraphicsStateGuardian *tinygsg;
  171. if (_gsg == 0) {
  172. // There is no old gsg. Create a new one.
  173. tinygsg = new TinyGraphicsStateGuardian(_engine, _pipe, nullptr);
  174. _gsg = tinygsg;
  175. } else {
  176. DCAST_INTO_R(tinygsg, _gsg, false);
  177. }
  178. _hdc = GetDC(_hWnd);
  179. create_frame_buffer();
  180. if (_frame_buffer == nullptr) {
  181. tinydisplay_cat.error()
  182. << "Could not create frame buffer.\n";
  183. return false;
  184. }
  185. tinygsg->_current_frame_buffer = _frame_buffer;
  186. tinygsg->reset_if_new();
  187. if (!tinygsg->is_valid()) {
  188. close_window();
  189. return false;
  190. }
  191. return true;
  192. }
  193. /**
  194. * Called in the window thread when the window size or location is changed,
  195. * this updates the properties structure accordingly.
  196. */
  197. void TinyWinGraphicsWindow::
  198. handle_reshape() {
  199. WinGraphicsWindow::handle_reshape();
  200. if (_frame_buffer != nullptr) {
  201. ZB_resize(_frame_buffer, nullptr, _properties.get_x_size(), _properties.get_y_size());
  202. setup_bitmap_info();
  203. }
  204. }
  205. /**
  206. * Called in the window thread when the window size or location is changed,
  207. * this updates the properties structure accordingly.
  208. */
  209. bool TinyWinGraphicsWindow::
  210. do_fullscreen_resize(int x_size, int y_size) {
  211. bool result = WinGraphicsWindow::do_fullscreen_resize(x_size, y_size);
  212. ZB_resize(_frame_buffer, nullptr, _properties.get_x_size(), _properties.get_y_size());
  213. setup_bitmap_info();
  214. return result;
  215. }
  216. /**
  217. * Creates a suitable frame buffer for the current window size.
  218. */
  219. void TinyWinGraphicsWindow::
  220. create_frame_buffer() {
  221. if (_frame_buffer != nullptr) {
  222. ZB_close(_frame_buffer);
  223. _frame_buffer = nullptr;
  224. }
  225. _frame_buffer = ZB_open(_properties.get_x_size(), _properties.get_y_size(), ZB_MODE_RGBA, 0, 0, 0, 0);
  226. setup_bitmap_info();
  227. }
  228. /**
  229. * Determines the BITMAPINFO stuff for blitting the frame buffer to the
  230. * window.
  231. */
  232. void TinyWinGraphicsWindow::
  233. setup_bitmap_info() {
  234. _bitmap_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  235. _bitmap_info.bmiHeader.biWidth = _frame_buffer->xsize;
  236. _bitmap_info.bmiHeader.biHeight = -_frame_buffer->ysize;
  237. _bitmap_info.bmiHeader.biPlanes = 1;
  238. _bitmap_info.bmiHeader.biBitCount = 32;
  239. _bitmap_info.bmiHeader.biCompression = BI_RGB;
  240. _bitmap_info.bmiHeader.biSizeImage = _frame_buffer->linesize * _frame_buffer->ysize;
  241. _bitmap_info.bmiHeader.biXPelsPerMeter = 0;
  242. _bitmap_info.bmiHeader.biYPelsPerMeter = 0;
  243. _bitmap_info.bmiHeader.biClrUsed = 0;
  244. _bitmap_info.bmiHeader.biClrImportant = 0;
  245. }
  246. #endif // WIN32