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