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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include <assert.h>
  19. #if !defined(WIN32) && !defined(__APPLE__)
  20. #include <sys/ipc.h>
  21. #include <sys/shm.h>
  22. #endif
  23. #include <FL/Fl.H>
  24. #include <FL/x.H>
  25. #include <rfb/LogWriter.h>
  26. #include <rdr/Exception.h>
  27. #include "PlatformPixelBuffer.h"
  28. static rfb::LogWriter vlog("PlatformPixelBuffer");
  29. PlatformPixelBuffer::PlatformPixelBuffer(int width, int height) :
  30. FullFramePixelBuffer(rfb::PixelFormat(32, 24,
  31. #if !defined(WIN32) && !defined(__APPLE__)
  32. ImageByteOrder(fl_display) == MSBFirst,
  33. #else
  34. false,
  35. #endif
  36. true, 255, 255, 255, 16, 8, 0),
  37. width, height, 0, stride),
  38. Surface(width, height)
  39. #if !defined(WIN32) && !defined(__APPLE__)
  40. , shminfo(NULL), xim(NULL)
  41. #endif
  42. {
  43. #if !defined(WIN32) && !defined(__APPLE__)
  44. if (!setupShm()) {
  45. xim = XCreateImage(fl_display, CopyFromParent, 32,
  46. ZPixmap, 0, 0, width, height, 32, 0);
  47. if (!xim)
  48. throw rdr::Exception("XCreateImage");
  49. xim->data = (char*)malloc(xim->bytes_per_line * xim->height);
  50. if (!xim->data)
  51. throw rdr::Exception("malloc");
  52. vlog.debug("Using standard XImage");
  53. }
  54. data = (rdr::U8*)xim->data;
  55. stride = xim->bytes_per_line / (getPF().bpp/8);
  56. #else
  57. FullFramePixelBuffer::data = (rdr::U8*)Surface::data;
  58. stride = 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. GC gc;
  94. gc = XCreateGC(fl_display, pixmap, 0, NULL);
  95. if (shminfo) {
  96. XShmPutImage(fl_display, pixmap, gc, xim,
  97. r.tl.x, r.tl.y, r.tl.x, r.tl.y,
  98. r.width(), r.height(), False);
  99. // Need to make sure the X server has finished reading the
  100. // shared memory before we return
  101. XSync(fl_display, False);
  102. } else {
  103. XPutImage(fl_display, pixmap, gc, xim,
  104. r.tl.x, r.tl.y, r.tl.x, r.tl.y, r.width(), r.height());
  105. }
  106. XFreeGC(fl_display, gc);
  107. #endif
  108. return r;
  109. }
  110. #if !defined(WIN32) && !defined(__APPLE__)
  111. static bool caughtError;
  112. static int XShmAttachErrorHandler(Display *dpy, XErrorEvent *error)
  113. {
  114. caughtError = true;
  115. return 0;
  116. }
  117. bool PlatformPixelBuffer::setupShm()
  118. {
  119. int major, minor;
  120. Bool pixmaps;
  121. XErrorHandler old_handler;
  122. const char *display_name = XDisplayName (NULL);
  123. /* Don't use MIT-SHM on remote displays */
  124. if (*display_name && *display_name != ':')
  125. return false;
  126. if (!XShmQueryVersion(fl_display, &major, &minor, &pixmaps))
  127. return false;
  128. shminfo = new XShmSegmentInfo;
  129. xim = XShmCreateImage(fl_display, CopyFromParent, 32,
  130. ZPixmap, 0, shminfo, width(), height());
  131. if (!xim)
  132. goto free_shminfo;
  133. shminfo->shmid = shmget(IPC_PRIVATE,
  134. xim->bytes_per_line * xim->height,
  135. IPC_CREAT|0600);
  136. if (shminfo->shmid == -1)
  137. goto free_xim;
  138. shminfo->shmaddr = xim->data = (char*)shmat(shminfo->shmid, 0, 0);
  139. shmctl(shminfo->shmid, IPC_RMID, 0); // to avoid memory leakage
  140. if (shminfo->shmaddr == (char *)-1)
  141. goto free_xim;
  142. shminfo->readOnly = True;
  143. // This is the only way we can detect that shared memory won't work
  144. // (e.g. because we're accessing a remote X11 server)
  145. caughtError = false;
  146. old_handler = XSetErrorHandler(XShmAttachErrorHandler);
  147. if (!XShmAttach(fl_display, shminfo)) {
  148. XSetErrorHandler(old_handler);
  149. goto free_shmaddr;
  150. }
  151. XSync(fl_display, False);
  152. XSetErrorHandler(old_handler);
  153. if (caughtError)
  154. goto free_shmaddr;
  155. vlog.debug("Using shared memory XImage");
  156. return true;
  157. free_shmaddr:
  158. shmdt(shminfo->shmaddr);
  159. free_xim:
  160. XDestroyImage(xim);
  161. xim = NULL;
  162. free_shminfo:
  163. delete shminfo;
  164. shminfo = NULL;
  165. return 0;
  166. }
  167. #endif