您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PlatformPixelBuffer.cxx 5.0KB

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