blob: c79b5c1a2521b930f1d67342af93545d2f10ebf1 [file] [log] [blame]
Pierre Ossman403ac272017-01-02 17:00:41 +01001/* Copyright 2011-2016 Pierre Ossman for Cendio AB
Pierre Ossmanac13abe2014-02-07 14:46:26 +01002 *
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 Ossman403ac272017-01-02 17:00:41 +010019#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 Ossmanac13abe2014-02-07 14:46:26 +010032#include "PlatformPixelBuffer.h"
33
Pierre Ossman403ac272017-01-02 17:00:41 +010034static rfb::LogWriter vlog("PlatformPixelBuffer");
35
36PlatformPixelBuffer::PlatformPixelBuffer(int width, int height) :
Jan Grulichcf21a802018-08-08 15:33:23 +020037 FullFramePixelBuffer(rfb::PixelFormat(32, 24,
38#if !defined(WIN32) && !defined(__APPLE__)
39 ImageByteOrder(fl_display) == MSBFirst,
40#else
41 false,
42#endif
Jan Grulich86cec812018-08-08 15:29:23 +020043 true, 255, 255, 255, 16, 8, 0),
Pierre Ossman403ac272017-01-02 17:00:41 +010044 width, height, 0, stride),
45 Surface(width, height)
46#if !defined(WIN32) && !defined(__APPLE__)
47 , shminfo(NULL), xim(NULL)
48#endif
Pierre Ossmanac13abe2014-02-07 14:46:26 +010049{
Pierre Ossman403ac272017-01-02 17:00:41 +010050#if !defined(WIN32) && !defined(__APPLE__)
51 if (!setupShm()) {
52 xim = XCreateImage(fl_display, CopyFromParent, 32,
53 ZPixmap, 0, 0, width, height, 32, 0);
54 if (!xim)
55 throw rdr::Exception("XCreateImage");
56
57 xim->data = (char*)malloc(xim->bytes_per_line * xim->height);
58 if (!xim->data)
59 throw rdr::Exception("malloc");
60
61 vlog.debug("Using standard XImage");
62 }
63
64 data = (rdr::U8*)xim->data;
65 stride = xim->bytes_per_line / (getPF().bpp/8);
66#else
67 FullFramePixelBuffer::data = (rdr::U8*)Surface::data;
68 stride = width;
69#endif
70}
71
72PlatformPixelBuffer::~PlatformPixelBuffer()
73{
74#if !defined(WIN32) && !defined(__APPLE__)
75 if (shminfo) {
76 vlog.debug("Freeing shared memory XImage");
Pierre Ossmanfbbd48b2017-03-29 13:27:32 +020077 XShmDetach(fl_display, shminfo);
Pierre Ossman403ac272017-01-02 17:00:41 +010078 shmdt(shminfo->shmaddr);
79 shmctl(shminfo->shmid, IPC_RMID, 0);
80 delete shminfo;
81 shminfo = NULL;
82 }
83
84 // XDestroyImage() will free(xim->data) if appropriate
85 if (xim)
86 XDestroyImage(xim);
87 xim = NULL;
88#endif
Pierre Ossmanac13abe2014-02-07 14:46:26 +010089}
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020090
91void PlatformPixelBuffer::commitBufferRW(const rfb::Rect& r)
92{
93 FullFramePixelBuffer::commitBufferRW(r);
Pierre Ossman0b5a06b2015-11-13 14:09:27 +010094 mutex.lock();
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020095 damage.assign_union(rfb::Region(r));
Pierre Ossman0b5a06b2015-11-13 14:09:27 +010096 mutex.unlock();
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020097}
98
99rfb::Rect PlatformPixelBuffer::getDamage(void)
100{
101 rfb::Rect r;
102
Pierre Ossman0b5a06b2015-11-13 14:09:27 +0100103 mutex.lock();
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200104 r = damage.get_bounding_rect();
105 damage.clear();
Pierre Ossman0b5a06b2015-11-13 14:09:27 +0100106 mutex.unlock();
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200107
Pierre Ossman403ac272017-01-02 17:00:41 +0100108#if !defined(WIN32) && !defined(__APPLE__)
109 GC gc;
110
111 gc = XCreateGC(fl_display, pixmap, 0, NULL);
112 if (shminfo) {
113 XShmPutImage(fl_display, pixmap, gc, xim,
114 r.tl.x, r.tl.y, r.tl.x, r.tl.y,
115 r.width(), r.height(), False);
116 // Need to make sure the X server has finished reading the
117 // shared memory before we return
118 XSync(fl_display, False);
119 } else {
120 XPutImage(fl_display, pixmap, gc, xim,
121 r.tl.x, r.tl.y, r.tl.x, r.tl.y, r.width(), r.height());
122 }
123 XFreeGC(fl_display, gc);
124#endif
125
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200126 return r;
127}
Pierre Ossman403ac272017-01-02 17:00:41 +0100128
129#if !defined(WIN32) && !defined(__APPLE__)
130
131static bool caughtError;
132
133static int XShmAttachErrorHandler(Display *dpy, XErrorEvent *error)
134{
135 caughtError = true;
136 return 0;
137}
138
139bool PlatformPixelBuffer::setupShm()
140{
141 int major, minor;
142 Bool pixmaps;
143 XErrorHandler old_handler;
144 const char *display_name = XDisplayName (NULL);
145
146 /* Don't use MIT-SHM on remote displays */
147 if (*display_name && *display_name != ':')
148 return false;
149
150 if (!XShmQueryVersion(fl_display, &major, &minor, &pixmaps))
151 return false;
152
153 shminfo = new XShmSegmentInfo;
154
155 xim = XShmCreateImage(fl_display, CopyFromParent, 32,
156 ZPixmap, 0, shminfo, width(), height());
157 if (!xim)
158 goto free_shminfo;
159
160 shminfo->shmid = shmget(IPC_PRIVATE,
161 xim->bytes_per_line * xim->height,
Pierre Ossmand71508b2017-03-29 13:28:55 +0200162 IPC_CREAT|0600);
Pierre Ossman403ac272017-01-02 17:00:41 +0100163 if (shminfo->shmid == -1)
164 goto free_xim;
165
166 shminfo->shmaddr = xim->data = (char*)shmat(shminfo->shmid, 0, 0);
167 shmctl(shminfo->shmid, IPC_RMID, 0); // to avoid memory leakage
168 if (shminfo->shmaddr == (char *)-1)
169 goto free_xim;
170
171 shminfo->readOnly = True;
172
173 // This is the only way we can detect that shared memory won't work
174 // (e.g. because we're accessing a remote X11 server)
175 caughtError = false;
176 old_handler = XSetErrorHandler(XShmAttachErrorHandler);
177
178 if (!XShmAttach(fl_display, shminfo)) {
179 XSetErrorHandler(old_handler);
180 goto free_shmaddr;
181 }
182
183 XSync(fl_display, False);
184
185 XSetErrorHandler(old_handler);
186
187 if (caughtError)
188 goto free_shmaddr;
189
190 vlog.debug("Using shared memory XImage");
191
192 return true;
193
194free_shmaddr:
195 shmdt(shminfo->shmaddr);
196
197free_xim:
198 XDestroyImage(xim);
199 xim = NULL;
200
201free_shminfo:
202 delete shminfo;
203 shminfo = NULL;
204
205 return 0;
206}
207
208#endif