Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 1 | /* Copyright 2011-2016 Pierre Ossman for Cendio AB |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 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 | |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 19 | #include <assert.h> |
| 20 | |
| 21 | #if !defined(WIN32) && !defined(__APPLE__) |
| 22 | #include <sys/ipc.h> |
| 23 | #include <sys/shm.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <FL/Fl.H> |
| 27 | #include <FL/x.H> |
| 28 | |
| 29 | #include <rfb/LogWriter.h> |
| 30 | #include <rdr/Exception.h> |
| 31 | |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 32 | #include "PlatformPixelBuffer.h" |
| 33 | |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 34 | static rfb::LogWriter vlog("PlatformPixelBuffer"); |
| 35 | |
| 36 | PlatformPixelBuffer::PlatformPixelBuffer(int width, int height) : |
Jan Grulich | 86cec81 | 2018-08-08 15:29:23 +0200 | [diff] [blame^] | 37 | FullFramePixelBuffer(rfb::PixelFormat(32, 24, ImageByteOrder(fl_display) == MSBFirst, |
| 38 | true, 255, 255, 255, 16, 8, 0), |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 39 | width, height, 0, stride), |
| 40 | Surface(width, height) |
| 41 | #if !defined(WIN32) && !defined(__APPLE__) |
| 42 | , shminfo(NULL), xim(NULL) |
| 43 | #endif |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 44 | { |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 45 | #if !defined(WIN32) && !defined(__APPLE__) |
| 46 | if (!setupShm()) { |
| 47 | xim = XCreateImage(fl_display, CopyFromParent, 32, |
| 48 | ZPixmap, 0, 0, width, height, 32, 0); |
| 49 | if (!xim) |
| 50 | throw rdr::Exception("XCreateImage"); |
| 51 | |
| 52 | xim->data = (char*)malloc(xim->bytes_per_line * xim->height); |
| 53 | if (!xim->data) |
| 54 | throw rdr::Exception("malloc"); |
| 55 | |
| 56 | vlog.debug("Using standard XImage"); |
| 57 | } |
| 58 | |
| 59 | data = (rdr::U8*)xim->data; |
| 60 | stride = xim->bytes_per_line / (getPF().bpp/8); |
| 61 | #else |
| 62 | FullFramePixelBuffer::data = (rdr::U8*)Surface::data; |
| 63 | stride = width; |
| 64 | #endif |
| 65 | } |
| 66 | |
| 67 | PlatformPixelBuffer::~PlatformPixelBuffer() |
| 68 | { |
| 69 | #if !defined(WIN32) && !defined(__APPLE__) |
| 70 | if (shminfo) { |
| 71 | vlog.debug("Freeing shared memory XImage"); |
Pierre Ossman | fbbd48b | 2017-03-29 13:27:32 +0200 | [diff] [blame] | 72 | XShmDetach(fl_display, shminfo); |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 73 | shmdt(shminfo->shmaddr); |
| 74 | shmctl(shminfo->shmid, IPC_RMID, 0); |
| 75 | delete shminfo; |
| 76 | shminfo = NULL; |
| 77 | } |
| 78 | |
| 79 | // XDestroyImage() will free(xim->data) if appropriate |
| 80 | if (xim) |
| 81 | XDestroyImage(xim); |
| 82 | xim = NULL; |
| 83 | #endif |
Pierre Ossman | ac13abe | 2014-02-07 14:46:26 +0100 | [diff] [blame] | 84 | } |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 85 | |
| 86 | void PlatformPixelBuffer::commitBufferRW(const rfb::Rect& r) |
| 87 | { |
| 88 | FullFramePixelBuffer::commitBufferRW(r); |
Pierre Ossman | 0b5a06b | 2015-11-13 14:09:27 +0100 | [diff] [blame] | 89 | mutex.lock(); |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 90 | damage.assign_union(rfb::Region(r)); |
Pierre Ossman | 0b5a06b | 2015-11-13 14:09:27 +0100 | [diff] [blame] | 91 | mutex.unlock(); |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | rfb::Rect PlatformPixelBuffer::getDamage(void) |
| 95 | { |
| 96 | rfb::Rect r; |
| 97 | |
Pierre Ossman | 0b5a06b | 2015-11-13 14:09:27 +0100 | [diff] [blame] | 98 | mutex.lock(); |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 99 | r = damage.get_bounding_rect(); |
| 100 | damage.clear(); |
Pierre Ossman | 0b5a06b | 2015-11-13 14:09:27 +0100 | [diff] [blame] | 101 | mutex.unlock(); |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 102 | |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 103 | #if !defined(WIN32) && !defined(__APPLE__) |
| 104 | GC gc; |
| 105 | |
| 106 | gc = XCreateGC(fl_display, pixmap, 0, NULL); |
| 107 | if (shminfo) { |
| 108 | XShmPutImage(fl_display, pixmap, gc, xim, |
| 109 | r.tl.x, r.tl.y, r.tl.x, r.tl.y, |
| 110 | r.width(), r.height(), False); |
| 111 | // Need to make sure the X server has finished reading the |
| 112 | // shared memory before we return |
| 113 | XSync(fl_display, False); |
| 114 | } else { |
| 115 | XPutImage(fl_display, pixmap, gc, xim, |
| 116 | r.tl.x, r.tl.y, r.tl.x, r.tl.y, r.width(), r.height()); |
| 117 | } |
| 118 | XFreeGC(fl_display, gc); |
| 119 | #endif |
| 120 | |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 121 | return r; |
| 122 | } |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 123 | |
| 124 | #if !defined(WIN32) && !defined(__APPLE__) |
| 125 | |
| 126 | static bool caughtError; |
| 127 | |
| 128 | static int XShmAttachErrorHandler(Display *dpy, XErrorEvent *error) |
| 129 | { |
| 130 | caughtError = true; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | bool PlatformPixelBuffer::setupShm() |
| 135 | { |
| 136 | int major, minor; |
| 137 | Bool pixmaps; |
| 138 | XErrorHandler old_handler; |
| 139 | const char *display_name = XDisplayName (NULL); |
| 140 | |
| 141 | /* Don't use MIT-SHM on remote displays */ |
| 142 | if (*display_name && *display_name != ':') |
| 143 | return false; |
| 144 | |
| 145 | if (!XShmQueryVersion(fl_display, &major, &minor, &pixmaps)) |
| 146 | return false; |
| 147 | |
| 148 | shminfo = new XShmSegmentInfo; |
| 149 | |
| 150 | xim = XShmCreateImage(fl_display, CopyFromParent, 32, |
| 151 | ZPixmap, 0, shminfo, width(), height()); |
| 152 | if (!xim) |
| 153 | goto free_shminfo; |
| 154 | |
| 155 | shminfo->shmid = shmget(IPC_PRIVATE, |
| 156 | xim->bytes_per_line * xim->height, |
Pierre Ossman | d71508b | 2017-03-29 13:28:55 +0200 | [diff] [blame] | 157 | IPC_CREAT|0600); |
Pierre Ossman | 403ac27 | 2017-01-02 17:00:41 +0100 | [diff] [blame] | 158 | if (shminfo->shmid == -1) |
| 159 | goto free_xim; |
| 160 | |
| 161 | shminfo->shmaddr = xim->data = (char*)shmat(shminfo->shmid, 0, 0); |
| 162 | shmctl(shminfo->shmid, IPC_RMID, 0); // to avoid memory leakage |
| 163 | if (shminfo->shmaddr == (char *)-1) |
| 164 | goto free_xim; |
| 165 | |
| 166 | shminfo->readOnly = True; |
| 167 | |
| 168 | // This is the only way we can detect that shared memory won't work |
| 169 | // (e.g. because we're accessing a remote X11 server) |
| 170 | caughtError = false; |
| 171 | old_handler = XSetErrorHandler(XShmAttachErrorHandler); |
| 172 | |
| 173 | if (!XShmAttach(fl_display, shminfo)) { |
| 174 | XSetErrorHandler(old_handler); |
| 175 | goto free_shmaddr; |
| 176 | } |
| 177 | |
| 178 | XSync(fl_display, False); |
| 179 | |
| 180 | XSetErrorHandler(old_handler); |
| 181 | |
| 182 | if (caughtError) |
| 183 | goto free_shmaddr; |
| 184 | |
| 185 | vlog.debug("Using shared memory XImage"); |
| 186 | |
| 187 | return true; |
| 188 | |
| 189 | free_shmaddr: |
| 190 | shmdt(shminfo->shmaddr); |
| 191 | |
| 192 | free_xim: |
| 193 | XDestroyImage(xim); |
| 194 | xim = NULL; |
| 195 | |
| 196 | free_shminfo: |
| 197 | delete shminfo; |
| 198 | shminfo = NULL; |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | #endif |