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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #include <assert.h>
  19. #include <FL/Fl_RGB_Image.H>
  20. #include <FL/x.H>
  21. #include <rdr/Exception.h>
  22. #include "Surface.h"
  23. void Surface::clear(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
  24. {
  25. XRenderColor color;
  26. color.red = (unsigned)r * 65535 / 255 * a / 255;
  27. color.green = (unsigned)g * 65535 / 255 * a / 255;
  28. color.blue = (unsigned)b * 65535 / 255 * a / 255;
  29. color.alpha = (unsigned)a * 65535 / 255;
  30. XRenderFillRectangle(fl_display, PictOpSrc, picture, &color,
  31. 0, 0, width(), height());
  32. }
  33. void Surface::draw(int src_x, int src_y, int x, int y, int w, int h)
  34. {
  35. Picture winPict;
  36. winPict = XRenderCreatePicture(fl_display, fl_window, visFormat, 0, NULL);
  37. XRenderComposite(fl_display, PictOpSrc, picture, None, winPict,
  38. src_x, src_y, 0, 0, x, y, w, h);
  39. XRenderFreePicture(fl_display, winPict);
  40. }
  41. void Surface::draw(Surface* dst, int src_x, int src_y, int x, int y, int w, int h)
  42. {
  43. XRenderComposite(fl_display, PictOpSrc, picture, None, dst->picture,
  44. src_x, src_y, 0, 0, x, y, w, h);
  45. }
  46. static Picture alpha_mask(int a)
  47. {
  48. Pixmap pixmap;
  49. XRenderPictFormat* format;
  50. XRenderPictureAttributes rep;
  51. Picture pict;
  52. XRenderColor color;
  53. if (a == 255)
  54. return None;
  55. pixmap = XCreatePixmap(fl_display, XDefaultRootWindow(fl_display),
  56. 1, 1, 8);
  57. format = XRenderFindStandardFormat(fl_display, PictStandardA8);
  58. rep.repeat = RepeatNormal;
  59. pict = XRenderCreatePicture(fl_display, pixmap, format, CPRepeat, &rep);
  60. XFreePixmap(fl_display, pixmap);
  61. color.alpha = (unsigned)a * 65535 / 255;
  62. XRenderFillRectangle(fl_display, PictOpSrc, pict, &color,
  63. 0, 0, 1, 1);
  64. return pict;
  65. }
  66. void Surface::blend(int src_x, int src_y, int x, int y, int w, int h, int a)
  67. {
  68. Picture winPict, alpha;
  69. winPict = XRenderCreatePicture(fl_display, fl_window, visFormat, 0, NULL);
  70. alpha = alpha_mask(a);
  71. XRenderComposite(fl_display, PictOpOver, picture, alpha, winPict,
  72. src_x, src_y, 0, 0, x, y, w, h);
  73. XRenderFreePicture(fl_display, winPict);
  74. if (alpha != None)
  75. XRenderFreePicture(fl_display, alpha);
  76. }
  77. void Surface::blend(Surface* dst, int src_x, int src_y, int x, int y, int w, int h, int a)
  78. {
  79. Picture alpha;
  80. alpha = alpha_mask(a);
  81. XRenderComposite(fl_display, PictOpOver, picture, alpha, dst->picture,
  82. src_x, src_y, 0, 0, x, y, w, h);
  83. if (alpha != None)
  84. XRenderFreePicture(fl_display, alpha);
  85. }
  86. void Surface::alloc()
  87. {
  88. XRenderPictFormat templ;
  89. XRenderPictFormat* format;
  90. // Might not be open at this point
  91. fl_open_display();
  92. pixmap = XCreatePixmap(fl_display, XDefaultRootWindow(fl_display),
  93. width(), height(), 32);
  94. // Our code assumes a BGRA byte order, regardless of what the endian
  95. // of the machine is or the native byte order of XImage, so make sure
  96. // we find such a format
  97. templ.type = PictTypeDirect;
  98. templ.depth = 32;
  99. if (XImageByteOrder(fl_display) == MSBFirst) {
  100. templ.direct.alpha = 0;
  101. templ.direct.red = 8;
  102. templ.direct.green = 16;
  103. templ.direct.blue = 24;
  104. } else {
  105. templ.direct.alpha = 24;
  106. templ.direct.red = 16;
  107. templ.direct.green = 8;
  108. templ.direct.blue = 0;
  109. }
  110. templ.direct.alphaMask = 0xff;
  111. templ.direct.redMask = 0xff;
  112. templ.direct.greenMask = 0xff;
  113. templ.direct.blueMask = 0xff;
  114. format = XRenderFindFormat(fl_display, PictFormatType | PictFormatDepth |
  115. PictFormatRed | PictFormatRedMask |
  116. PictFormatGreen | PictFormatGreenMask |
  117. PictFormatBlue | PictFormatBlueMask |
  118. PictFormatAlpha | PictFormatAlphaMask,
  119. &templ, 0);
  120. if (!format)
  121. throw rdr::Exception("XRenderFindFormat");
  122. picture = XRenderCreatePicture(fl_display, pixmap, format, 0, NULL);
  123. visFormat = XRenderFindVisualFormat(fl_display, fl_visual->visual);
  124. }
  125. void Surface::dealloc()
  126. {
  127. XRenderFreePicture(fl_display, picture);
  128. XFreePixmap(fl_display, pixmap);
  129. }
  130. void Surface::update(const Fl_RGB_Image* image)
  131. {
  132. XImage* img;
  133. GC gc;
  134. int x, y;
  135. const unsigned char* in;
  136. unsigned char* out;
  137. assert(image->w() == width());
  138. assert(image->h() == height());
  139. img = XCreateImage(fl_display, CopyFromParent, 32,
  140. ZPixmap, 0, NULL, width(), height(),
  141. 32, 0);
  142. if (!img)
  143. throw rdr::Exception("XCreateImage");
  144. img->data = (char*)malloc(img->bytes_per_line * img->height);
  145. if (!img->data)
  146. throw rdr::Exception("malloc");
  147. // Convert data and pre-multiply alpha
  148. in = (const unsigned char*)image->data()[0];
  149. out = (unsigned char*)img->data;
  150. for (y = 0;y < img->height;y++) {
  151. for (x = 0;x < img->width;x++) {
  152. switch (image->d()) {
  153. case 1:
  154. *out++ = in[0];
  155. *out++ = in[0];
  156. *out++ = in[0];
  157. *out++ = 0xff;
  158. break;
  159. case 2:
  160. *out++ = (unsigned)in[0] * in[1] / 255;
  161. *out++ = (unsigned)in[0] * in[1] / 255;
  162. *out++ = (unsigned)in[0] * in[1] / 255;
  163. *out++ = in[1];
  164. break;
  165. case 3:
  166. *out++ = in[2];
  167. *out++ = in[1];
  168. *out++ = in[0];
  169. *out++ = 0xff;
  170. break;
  171. case 4:
  172. *out++ = (unsigned)in[2] * in[3] / 255;
  173. *out++ = (unsigned)in[1] * in[3] / 255;
  174. *out++ = (unsigned)in[0] * in[3] / 255;
  175. *out++ = in[3];
  176. break;
  177. }
  178. in += image->d();
  179. }
  180. if (image->ld() != 0)
  181. in += image->ld() - image->w() * image->d();
  182. }
  183. gc = XCreateGC(fl_display, pixmap, 0, NULL);
  184. XPutImage(fl_display, pixmap, gc, img,
  185. 0, 0, 0, 0, img->width, img->height);
  186. XFreeGC(fl_display, gc);
  187. XDestroyImage(img);
  188. }