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.

PlatformPixelBuffer.cxx 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* Copyright 2011-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. #if !defined(WIN32) && !defined(__APPLE__)
  24. #include <sys/ipc.h>
  25. #include <sys/shm.h>
  26. #endif
  27. #include <FL/Fl.H>
  28. #include <FL/x.H>
  29. #include <rfb/LogWriter.h>
  30. #include <rdr/Exception.h>
  31. #include "PlatformPixelBuffer.h"
  32. static rfb::LogWriter vlog("PlatformPixelBuffer");
  33. PlatformPixelBuffer::PlatformPixelBuffer(int width, int height) :
  34. FullFramePixelBuffer(rfb::PixelFormat(32, 24, false, true,
  35. 255, 255, 255, 16, 8, 0),
  36. 0, 0, NULL, 0),
  37. Surface(width, height)
  38. #if !defined(WIN32) && !defined(__APPLE__)
  39. , shminfo(NULL), xim(NULL)
  40. #endif
  41. {
  42. #if !defined(WIN32) && !defined(__APPLE__)
  43. if (!setupShm(width, height)) {
  44. xim = XCreateImage(fl_display, CopyFromParent, 32,
  45. ZPixmap, 0, 0, width, height, 32, 0);
  46. if (!xim)
  47. throw rdr::Exception("XCreateImage");
  48. xim->data = (char*)malloc(xim->bytes_per_line * xim->height);
  49. if (!xim->data)
  50. throw rdr::Exception("malloc");
  51. vlog.debug("Using standard XImage");
  52. }
  53. setBuffer(width, height, (uint8_t*)xim->data,
  54. xim->bytes_per_line / (getPF().bpp/8));
  55. // On X11, the Pixmap backing this Surface is uninitialized.
  56. clear(0, 0, 0);
  57. #else
  58. setBuffer(width, height, (uint8_t*)Surface::data, width);
  59. #endif
  60. }
  61. PlatformPixelBuffer::~PlatformPixelBuffer()
  62. {
  63. #if !defined(WIN32) && !defined(__APPLE__)
  64. if (shminfo) {
  65. vlog.debug("Freeing shared memory XImage");
  66. XShmDetach(fl_display, shminfo);
  67. shmdt(shminfo->shmaddr);
  68. shmctl(shminfo->shmid, IPC_RMID, 0);
  69. delete shminfo;
  70. shminfo = NULL;
  71. }
  72. // XDestroyImage() will free(xim->data) if appropriate
  73. if (xim)
  74. XDestroyImage(xim);
  75. xim = NULL;
  76. #endif
  77. }
  78. void PlatformPixelBuffer::commitBufferRW(const rfb::Rect& r)
  79. {
  80. FullFramePixelBuffer::commitBufferRW(r);
  81. mutex.lock();
  82. damage.assign_union(rfb::Region(r));
  83. mutex.unlock();
  84. }
  85. rfb::Rect PlatformPixelBuffer::getDamage(void)
  86. {
  87. rfb::Rect r;
  88. mutex.lock();
  89. r = damage.get_bounding_rect();
  90. damage.clear();
  91. mutex.unlock();
  92. #if !defined(WIN32) && !defined(__APPLE__)
  93. if (r.width() == 0 || r.height() == 0)
  94. return r;
  95. GC gc;
  96. gc = XCreateGC(fl_display, pixmap, 0, NULL);
  97. if (shminfo) {
  98. XShmPutImage(fl_display, pixmap, gc, xim,
  99. r.tl.x, r.tl.y, r.tl.x, r.tl.y,
  100. r.width(), r.height(), False);
  101. // Need to make sure the X server has finished reading the
  102. // shared memory before we return
  103. XSync(fl_display, False);
  104. } else {
  105. XPutImage(fl_display, pixmap, gc, xim,
  106. r.tl.x, r.tl.y, r.tl.x, r.tl.y, r.width(), r.height());
  107. }
  108. XFreeGC(fl_display, gc);
  109. #endif
  110. return r;
  111. }
  112. #if !defined(WIN32) && !defined(__APPLE__)
  113. static bool caughtError;
  114. static int XShmAttachErrorHandler(Display* /*dpy*/,
  115. XErrorEvent* /*error*/)
  116. {
  117. caughtError = true;
  118. return 0;
  119. }
  120. bool PlatformPixelBuffer::setupShm(int width, int height)
  121. {
  122. int major, minor;
  123. Bool pixmaps;
  124. XErrorHandler old_handler;
  125. const char *display_name = XDisplayName (NULL);
  126. /* Don't use MIT-SHM on remote displays */
  127. if (*display_name && *display_name != ':')
  128. return false;
  129. if (!XShmQueryVersion(fl_display, &major, &minor, &pixmaps))
  130. return false;
  131. shminfo = new XShmSegmentInfo;
  132. xim = XShmCreateImage(fl_display, CopyFromParent, 32,
  133. ZPixmap, 0, shminfo, width, height);
  134. if (!xim)
  135. goto free_shminfo;
  136. shminfo->shmid = shmget(IPC_PRIVATE,
  137. xim->bytes_per_line * xim->height,
  138. IPC_CREAT|0600);
  139. if (shminfo->shmid == -1)
  140. goto free_xim;
  141. shminfo->shmaddr = xim->data = (char*)shmat(shminfo->shmid, 0, 0);
  142. shmctl(shminfo->shmid, IPC_RMID, 0); // to avoid memory leakage
  143. if (shminfo->shmaddr == (char *)-1)
  144. goto free_xim;
  145. shminfo->readOnly = True;
  146. // This is the only way we can detect that shared memory won't work
  147. // (e.g. because we're accessing a remote X11 server)
  148. caughtError = false;
  149. old_handler = XSetErrorHandler(XShmAttachErrorHandler);
  150. if (!XShmAttach(fl_display, shminfo)) {
  151. XSetErrorHandler(old_handler);
  152. goto free_shmaddr;
  153. }
  154. XSync(fl_display, False);
  155. XSetErrorHandler(old_handler);
  156. if (caughtError)
  157. goto free_shmaddr;
  158. vlog.debug("Using shared memory XImage");
  159. return true;
  160. free_shmaddr:
  161. shmdt(shminfo->shmaddr);
  162. free_xim:
  163. XDestroyImage(xim);
  164. xim = NULL;
  165. free_shminfo:
  166. delete shminfo;
  167. shminfo = NULL;
  168. return 0;
  169. }
  170. #endif