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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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* format;
  89. // Might not be open at this point
  90. fl_open_display();
  91. pixmap = XCreatePixmap(fl_display, XDefaultRootWindow(fl_display),
  92. width(), height(), 32);
  93. format = XRenderFindStandardFormat(fl_display, PictStandardARGB32);
  94. picture = XRenderCreatePicture(fl_display, pixmap, format, 0, NULL);
  95. visFormat = XRenderFindVisualFormat(fl_display, fl_visual->visual);
  96. }
  97. void Surface::dealloc()
  98. {
  99. XRenderFreePicture(fl_display, picture);
  100. XFreePixmap(fl_display, pixmap);
  101. }
  102. void Surface::update(const Fl_RGB_Image* image)
  103. {
  104. XImage* img;
  105. GC gc;
  106. int x, y;
  107. const unsigned char* in;
  108. unsigned char* out;
  109. assert(image->w() == width());
  110. assert(image->h() == height());
  111. img = XCreateImage(fl_display, CopyFromParent, 32,
  112. ZPixmap, 0, NULL, width(), height(),
  113. 32, 0);
  114. if (!img)
  115. throw rdr::Exception("XCreateImage");
  116. img->data = (char*)malloc(img->bytes_per_line * img->height);
  117. if (!img->data)
  118. throw rdr::Exception("malloc");
  119. // Convert data and pre-multiply alpha
  120. in = (const unsigned char*)image->data()[0];
  121. out = (unsigned char*)img->data;
  122. for (y = 0;y < img->height;y++) {
  123. for (x = 0;x < img->width;x++) {
  124. switch (image->d()) {
  125. case 1:
  126. *out++ = in[0];
  127. *out++ = in[0];
  128. *out++ = in[0];
  129. *out++ = 0xff;
  130. break;
  131. case 2:
  132. *out++ = (unsigned)in[0] * in[1] / 255;
  133. *out++ = (unsigned)in[0] * in[1] / 255;
  134. *out++ = (unsigned)in[0] * in[1] / 255;
  135. *out++ = in[1];
  136. break;
  137. case 3:
  138. *out++ = in[2];
  139. *out++ = in[1];
  140. *out++ = in[0];
  141. *out++ = 0xff;
  142. break;
  143. case 4:
  144. *out++ = (unsigned)in[2] * in[3] / 255;
  145. *out++ = (unsigned)in[1] * in[3] / 255;
  146. *out++ = (unsigned)in[0] * in[3] / 255;
  147. *out++ = in[3];
  148. break;
  149. }
  150. in += image->d();
  151. }
  152. if (image->ld() != 0)
  153. in += image->ld() - image->w() * image->d();
  154. }
  155. gc = XCreateGC(fl_display, pixmap, 0, NULL);
  156. XPutImage(fl_display, pixmap, gc, img,
  157. 0, 0, 0, 0, img->width, img->height);
  158. XFreeGC(fl_display, gc);
  159. XDestroyImage(img);
  160. }