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

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