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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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::alloc()
  42. {
  43. XRenderPictFormat* format;
  44. // Might not be open at this point
  45. fl_open_display();
  46. pixmap = XCreatePixmap(fl_display, XDefaultRootWindow(fl_display),
  47. width(), height(), 32);
  48. format = XRenderFindStandardFormat(fl_display, PictStandardARGB32);
  49. picture = XRenderCreatePicture(fl_display, pixmap, format, 0, NULL);
  50. visFormat = XRenderFindVisualFormat(fl_display, fl_visual->visual);
  51. }
  52. void Surface::dealloc()
  53. {
  54. XRenderFreePicture(fl_display, picture);
  55. XFreePixmap(fl_display, pixmap);
  56. }
  57. void Surface::update(const Fl_RGB_Image* image)
  58. {
  59. XImage* img;
  60. GC gc;
  61. int x, y;
  62. const unsigned char* in;
  63. unsigned char* out;
  64. assert(image->w() == width());
  65. assert(image->h() == height());
  66. img = XCreateImage(fl_display, CopyFromParent, 32,
  67. ZPixmap, 0, NULL, width(), height(),
  68. 32, 0);
  69. if (!img)
  70. throw rdr::Exception("XCreateImage");
  71. img->data = (char*)malloc(img->bytes_per_line * img->height);
  72. if (!img->data)
  73. throw rdr::Exception("malloc");
  74. // Convert data and pre-multiply alpha
  75. in = (const unsigned char*)image->data()[0];
  76. out = (unsigned char*)img->data;
  77. for (y = 0;y < img->height;y++) {
  78. for (x = 0;x < img->width;x++) {
  79. switch (image->d()) {
  80. case 1:
  81. *out++ = in[0];
  82. *out++ = in[0];
  83. *out++ = in[0];
  84. *out++ = 0xff;
  85. break;
  86. case 2:
  87. *out++ = (unsigned)in[0] * in[1] / 255;
  88. *out++ = (unsigned)in[0] * in[1] / 255;
  89. *out++ = (unsigned)in[0] * in[1] / 255;
  90. *out++ = in[1];
  91. break;
  92. case 3:
  93. *out++ = in[2];
  94. *out++ = in[1];
  95. *out++ = in[0];
  96. *out++ = 0xff;
  97. break;
  98. case 4:
  99. *out++ = (unsigned)in[2] * in[3] / 255;
  100. *out++ = (unsigned)in[1] * in[3] / 255;
  101. *out++ = (unsigned)in[0] * in[3] / 255;
  102. *out++ = in[3];
  103. break;
  104. }
  105. in += image->d();
  106. }
  107. if (image->ld() != 0)
  108. in += image->ld() - image->w() * image->d();
  109. }
  110. gc = XCreateGC(fl_display, pixmap, 0, NULL);
  111. XPutImage(fl_display, pixmap, gc, img,
  112. 0, 0, 0, 0, img->width, img->height);
  113. XFreeGC(fl_display, gc);
  114. XDestroyImage(img);
  115. }