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

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