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.

Surface_X11.cxx 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Copyright 2016 Pierre Ossman for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <assert.h>
  22. #include <FL/Fl_RGB_Image.H>
  23. #include <FL/x.H>
  24. #include <rdr/Exception.h>
  25. #include "Surface.h"
  26. void Surface::clear(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
  27. {
  28. XRenderColor color;
  29. color.red = (unsigned)r * 65535 / 255 * a / 255;
  30. color.green = (unsigned)g * 65535 / 255 * a / 255;
  31. color.blue = (unsigned)b * 65535 / 255 * a / 255;
  32. color.alpha = (unsigned)a * 65535 / 255;
  33. XRenderFillRectangle(fl_display, PictOpSrc, picture, &color,
  34. 0, 0, width(), height());
  35. }
  36. void Surface::draw(int src_x, int src_y, int x, int y, int w, int h)
  37. {
  38. Picture winPict;
  39. winPict = XRenderCreatePicture(fl_display, fl_window, visFormat, 0, NULL);
  40. XRenderComposite(fl_display, PictOpSrc, picture, None, winPict,
  41. src_x, src_y, 0, 0, x, y, w, h);
  42. XRenderFreePicture(fl_display, winPict);
  43. }
  44. void Surface::draw(Surface* dst, int src_x, int src_y, int x, int y, int w, int h)
  45. {
  46. XRenderComposite(fl_display, PictOpSrc, picture, None, dst->picture,
  47. src_x, src_y, 0, 0, x, y, w, h);
  48. }
  49. static Picture alpha_mask(int a)
  50. {
  51. Pixmap pixmap;
  52. XRenderPictFormat* format;
  53. XRenderPictureAttributes rep;
  54. Picture pict;
  55. XRenderColor color;
  56. if (a == 255)
  57. return None;
  58. pixmap = XCreatePixmap(fl_display, XDefaultRootWindow(fl_display),
  59. 1, 1, 8);
  60. format = XRenderFindStandardFormat(fl_display, PictStandardA8);
  61. rep.repeat = RepeatNormal;
  62. pict = XRenderCreatePicture(fl_display, pixmap, format, CPRepeat, &rep);
  63. XFreePixmap(fl_display, pixmap);
  64. color.alpha = (unsigned)a * 65535 / 255;
  65. XRenderFillRectangle(fl_display, PictOpSrc, pict, &color,
  66. 0, 0, 1, 1);
  67. return pict;
  68. }
  69. void Surface::blend(int src_x, int src_y, int x, int y, int w, int h, int a)
  70. {
  71. Picture winPict, alpha;
  72. winPict = XRenderCreatePicture(fl_display, fl_window, visFormat, 0, NULL);
  73. alpha = alpha_mask(a);
  74. XRenderComposite(fl_display, PictOpOver, picture, alpha, winPict,
  75. src_x, src_y, 0, 0, x, y, w, h);
  76. XRenderFreePicture(fl_display, winPict);
  77. if (alpha != None)
  78. XRenderFreePicture(fl_display, alpha);
  79. }
  80. void Surface::blend(Surface* dst, int src_x, int src_y, int x, int y, int w, int h, int a)
  81. {
  82. Picture alpha;
  83. alpha = alpha_mask(a);
  84. XRenderComposite(fl_display, PictOpOver, picture, alpha, dst->picture,
  85. src_x, src_y, 0, 0, x, y, w, h);
  86. if (alpha != None)
  87. XRenderFreePicture(fl_display, alpha);
  88. }
  89. void Surface::alloc()
  90. {
  91. XRenderPictFormat templ;
  92. XRenderPictFormat* format;
  93. // Might not be open at this point
  94. fl_open_display();
  95. pixmap = XCreatePixmap(fl_display, XDefaultRootWindow(fl_display),
  96. width(), height(), 32);
  97. // Our code assumes a BGRA byte order, regardless of what the endian
  98. // of the machine is or the native byte order of XImage, so make sure
  99. // we find such a format
  100. templ.type = PictTypeDirect;
  101. templ.depth = 32;
  102. if (XImageByteOrder(fl_display) == MSBFirst) {
  103. templ.direct.alpha = 0;
  104. templ.direct.red = 8;
  105. templ.direct.green = 16;
  106. templ.direct.blue = 24;
  107. } else {
  108. templ.direct.alpha = 24;
  109. templ.direct.red = 16;
  110. templ.direct.green = 8;
  111. templ.direct.blue = 0;
  112. }
  113. templ.direct.alphaMask = 0xff;
  114. templ.direct.redMask = 0xff;
  115. templ.direct.greenMask = 0xff;
  116. templ.direct.blueMask = 0xff;
  117. format = XRenderFindFormat(fl_display, PictFormatType | PictFormatDepth |
  118. PictFormatRed | PictFormatRedMask |
  119. PictFormatGreen | PictFormatGreenMask |
  120. PictFormatBlue | PictFormatBlueMask |
  121. PictFormatAlpha | PictFormatAlphaMask,
  122. &templ, 0);
  123. if (!format)
  124. throw rdr::Exception("XRenderFindFormat");
  125. picture = XRenderCreatePicture(fl_display, pixmap, format, 0, NULL);
  126. visFormat = XRenderFindVisualFormat(fl_display, fl_visual->visual);
  127. }
  128. void Surface::dealloc()
  129. {
  130. XRenderFreePicture(fl_display, picture);
  131. XFreePixmap(fl_display, pixmap);
  132. }
  133. void Surface::update(const Fl_RGB_Image* image)
  134. {
  135. XImage* img;
  136. GC gc;
  137. int x, y;
  138. const unsigned char* in;
  139. unsigned char* out;
  140. assert(image->w() == width());
  141. assert(image->h() == height());
  142. img = XCreateImage(fl_display, CopyFromParent, 32,
  143. ZPixmap, 0, NULL, width(), height(),
  144. 32, 0);
  145. if (!img)
  146. throw rdr::Exception("XCreateImage");
  147. img->data = (char*)malloc(img->bytes_per_line * img->height);
  148. if (!img->data)
  149. throw rdr::Exception("malloc");
  150. // Convert data and pre-multiply alpha
  151. in = (const unsigned char*)image->data()[0];
  152. out = (unsigned char*)img->data;
  153. for (y = 0;y < img->height;y++) {
  154. for (x = 0;x < img->width;x++) {
  155. switch (image->d()) {
  156. case 1:
  157. *out++ = in[0];
  158. *out++ = in[0];
  159. *out++ = in[0];
  160. *out++ = 0xff;
  161. break;
  162. case 2:
  163. *out++ = (unsigned)in[0] * in[1] / 255;
  164. *out++ = (unsigned)in[0] * in[1] / 255;
  165. *out++ = (unsigned)in[0] * in[1] / 255;
  166. *out++ = in[1];
  167. break;
  168. case 3:
  169. *out++ = in[2];
  170. *out++ = in[1];
  171. *out++ = in[0];
  172. *out++ = 0xff;
  173. break;
  174. case 4:
  175. *out++ = (unsigned)in[2] * in[3] / 255;
  176. *out++ = (unsigned)in[1] * in[3] / 255;
  177. *out++ = (unsigned)in[0] * in[3] / 255;
  178. *out++ = in[3];
  179. break;
  180. }
  181. in += image->d();
  182. }
  183. if (image->ld() != 0)
  184. in += image->ld() - image->w() * image->d();
  185. }
  186. gc = XCreateGC(fl_display, pixmap, 0, NULL);
  187. XPutImage(fl_display, pixmap, gc, img,
  188. 0, 0, 0, 0, img->width, img->height);
  189. XFreeGC(fl_display, gc);
  190. XDestroyImage(img);
  191. }