You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.2KB

  1. //
  2. // "$Id: Fl_Gl_Device_Plugin.cxx 8368 2011-02-04 23:32:53Z manolo $"
  3. //
  4. // implementation of class Fl_Gl_Device_Plugin for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. #include <config.h>
  28. #include <FL/Fl_Printer.H>
  29. #include <FL/Fl_Gl_Window.H>
  30. #include "Fl_Gl_Choice.H"
  31. #include "FL/Fl.H"
  32. #ifndef __APPLE__
  33. #include "FL/fl_draw.H"
  34. #endif
  35. #if defined(__APPLE__)
  36. static void imgProviderReleaseData (void *info, const void *data, size_t size)
  37. {
  38. free((void *)data);
  39. }
  40. #endif
  41. static void print_gl_window(Fl_Gl_Window *glw, int x, int y, int height)
  42. {
  43. #ifdef WIN32
  44. HDC save_gc = fl_gc;
  45. const int bytesperpixel = 3;
  46. #elif defined(__APPLE__)
  47. CGContextRef save_gc = fl_gc;
  48. const int bytesperpixel = 4;
  49. #else
  50. _XGC *save_gc = fl_gc;
  51. const int bytesperpixel = 3;
  52. #endif
  53. Fl_Surface_Device *save_surface = Fl_Surface_Device::surface();
  54. fl_gc = NULL;
  55. Fl_Display_Device::display_device()->set_current();
  56. #ifdef WIN32
  57. Fl::check();
  58. Fl_Window *win = (Fl_Window*)glw;
  59. while( win->window() ) win = win->window();
  60. win->redraw();
  61. Fl::check();
  62. glw->make_current();
  63. #else
  64. glw->make_current();
  65. glw->redraw();
  66. glFlush();
  67. Fl::check();
  68. glFinish();
  69. #endif
  70. // Read OpenGL context pixels directly.
  71. // For extra safety, save & restore OpenGL states that are changed
  72. glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
  73. glPixelStorei(GL_PACK_ALIGNMENT, 4); /* Force 4-byte alignment */
  74. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  75. glPixelStorei(GL_PACK_SKIP_ROWS, 0);
  76. glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
  77. // Read a block of pixels from the frame buffer
  78. int mByteWidth = glw->w() * bytesperpixel;
  79. mByteWidth = (mByteWidth + 3) & ~3; // Align to 4 bytes
  80. uchar *baseAddress = (uchar*)malloc(mByteWidth * glw->h());
  81. glReadPixels(0, 0, glw->w(), glw->h(),
  82. #ifdef WIN32
  83. GL_RGB, GL_UNSIGNED_BYTE,
  84. #elif defined(__APPLE__)
  85. GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
  86. #else // FIXME Linux/Unix
  87. GL_RGB, GL_UNSIGNED_BYTE,
  88. #endif
  89. baseAddress);
  90. glPopClientAttrib();
  91. save_surface->set_current();
  92. fl_gc = save_gc;
  93. #if defined(__APPLE__)
  94. // kCGBitmapByteOrder32Host and CGBitmapInfo are supposed to arrive with 10.4
  95. // but some 10.4 don't have kCGBitmapByteOrder32Host, so we play a little #define game
  96. #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
  97. #define kCGBitmapByteOrder32Host 0
  98. #define CGBitmapInfo CGImageAlphaInfo
  99. #elif ! defined(kCGBitmapByteOrder32Host)
  100. #ifdef __BIG_ENDIAN__
  101. #define kCGBitmapByteOrder32Host (4 << 12)
  102. #else /* Little endian. */
  103. #define kCGBitmapByteOrder32Host (2 << 12)
  104. #endif
  105. #endif
  106. CGColorSpaceRef cSpace = CGColorSpaceCreateDeviceRGB();
  107. CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, baseAddress, mByteWidth * glw->h(), imgProviderReleaseData);
  108. CGImageRef image = CGImageCreate(glw->w(), glw->h(), 8, 8*bytesperpixel, mByteWidth, cSpace,
  109. (CGBitmapInfo)(kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host),
  110. provider, NULL, false, kCGRenderingIntentDefault);
  111. if(image == NULL) return;
  112. CGContextSaveGState(fl_gc);
  113. CGContextTranslateCTM(fl_gc, 0, height);
  114. CGContextScaleCTM(fl_gc, 1.0f, -1.0f);
  115. CGRect rect = { { x, height - y - glw->h() }, { glw->w(), glw->h() } };
  116. Fl_X::q_begin_image(rect, 0, 0, glw->w(), glw->h());
  117. CGContextDrawImage(fl_gc, rect, image);
  118. Fl_X::q_end_image();
  119. CGContextRestoreGState(fl_gc);
  120. CGImageRelease(image);
  121. CGColorSpaceRelease(cSpace);
  122. CGDataProviderRelease(provider);
  123. #else
  124. fl_draw_image(baseAddress + (glw->h() - 1) * mByteWidth, x, y , glw->w(), glw->h(), bytesperpixel, - mByteWidth);
  125. free(baseAddress);
  126. #endif // __APPLE__
  127. }
  128. /**
  129. This class will make sure that OpenGL printing is available if fltk_gl
  130. was linked to the program.
  131. */
  132. class Fl_Gl_Device_Plugin : public Fl_Device_Plugin {
  133. public:
  134. Fl_Gl_Device_Plugin() : Fl_Device_Plugin(name()) { }
  135. virtual const char *name() { return "opengl.device.fltk.org"; }
  136. virtual int print(Fl_Widget *w, int x, int y, int height) {
  137. Fl_Gl_Window *glw = w->as_gl_window();
  138. if (!glw) return 0;
  139. print_gl_window(glw, x, y, height);
  140. return 1;
  141. }
  142. };
  143. static Fl_Gl_Device_Plugin Gl_Device_Plugin;
  144. // The purpose of this variable, used in Fl_Gl_Window.cxx, is only to force this file to be loaded
  145. // whenever Fl_Gl_Window.cxx is loaded, that is, whenever fltk_gl is.
  146. FL_EXPORT int fl_gl_load_plugin = 0;
  147. //
  148. // End of "$Id: Fl_Gl_Device_Plugin.cxx 8368 2011-02-04 23:32:53Z manolo $".
  149. //