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

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