blob: ff1935e74765d73d69fc398a56ac6301fed64e36 [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) :
Pierre Ossmancc647db2018-10-25 10:36:21 +020037 FullFramePixelBuffer(rfb::PixelFormat(32, 24, false, true,
38 255, 255, 255, 16, 8, 0),
Pierre Ossman44cf1d62019-03-25 16:14:49 +010039 width, height, NULL, 0),
Pierre Ossman403ac272017-01-02 17:00:41 +010040 Surface(width, height)
41#if !defined(WIN32) && !defined(__APPLE__)
42 , shminfo(NULL), xim(NULL)
43#endif
Pierre Ossmanac13abe2014-02-07 14:46:26 +010044{
Pierre Ossman403ac272017-01-02 17:00:41 +010045#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);
Christian Authmann82b04eb2018-11-11 10:40:48 +010061
62 // On X11, the Pixmap backing this Surface is uninitialized.
63 clear(0, 0, 0);
Pierre Ossman403ac272017-01-02 17:00:41 +010064#else
65 FullFramePixelBuffer::data = (rdr::U8*)Surface::data;
66 stride = width;
67#endif
68}
69
70PlatformPixelBuffer::~PlatformPixelBuffer()
71{
72#if !defined(WIN32) && !defined(__APPLE__)
73 if (shminfo) {
74 vlog.debug("Freeing shared memory XImage");
Pierre Ossmanfbbd48b2017-03-29 13:27:32 +020075 XShmDetach(fl_display, shminfo);
Pierre Ossman403ac272017-01-02 17:00:41 +010076 shmdt(shminfo->shmaddr);
77 shmctl(shminfo->shmid, IPC_RMID, 0);
78 delete shminfo;
79 shminfo = NULL;
80 }
81
82 // XDestroyImage() will free(xim->data) if appropriate
83 if (xim)
84 XDestroyImage(xim);
85 xim = NULL;
86#endif
Pierre Ossmanac13abe2014-02-07 14:46:26 +010087}
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020088
89void PlatformPixelBuffer::commitBufferRW(const rfb::Rect& r)
90{
91 FullFramePixelBuffer::commitBufferRW(r);
Pierre Ossman0b5a06b2015-11-13 14:09:27 +010092 mutex.lock();
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020093 damage.assign_union(rfb::Region(r));
Pierre Ossman0b5a06b2015-11-13 14:09:27 +010094 mutex.unlock();
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020095}
96
97rfb::Rect PlatformPixelBuffer::getDamage(void)
98{
99 rfb::Rect r;
100
Pierre Ossman0b5a06b2015-11-13 14:09:27 +0100101 mutex.lock();
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200102 r = damage.get_bounding_rect();
103 damage.clear();
Pierre Ossman0b5a06b2015-11-13 14:09:27 +0100104 mutex.unlock();
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200105
Pierre Ossman403ac272017-01-02 17:00:41 +0100106#if !defined(WIN32) && !defined(__APPLE__)
Christian Authmannccc347d2018-11-11 11:14:40 +0100107 if (r.width() == 0 || r.height() == 0)
108 return r;
109
Pierre Ossman403ac272017-01-02 17:00:41 +0100110 GC gc;
111
112 gc = XCreateGC(fl_display, pixmap, 0, NULL);
113 if (shminfo) {
114 XShmPutImage(fl_display, pixmap, gc, xim,
115 r.tl.x, r.tl.y, r.tl.x, r.tl.y,
116 r.width(), r.height(), False);
117 // Need to make sure the X server has finished reading the
118 // shared memory before we return
119 XSync(fl_display, False);
120 } else {
121 XPutImage(fl_display, pixmap, gc, xim,
122 r.tl.x, r.tl.y, r.tl.x, r.tl.y, r.width(), r.height());
123 }
124 XFreeGC(fl_display, gc);
125#endif
126
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200127 return r;
128}
Pierre Ossman403ac272017-01-02 17:00:41 +0100129
130#if !defined(WIN32) && !defined(__APPLE__)
131
132static bool caughtError;
133
134static int XShmAttachErrorHandler(Display *dpy, XErrorEvent *error)
135{
136 caughtError = true;
137 return 0;
138}
139
140bool PlatformPixelBuffer::setupShm()
141{
142 int major, minor;
143 Bool pixmaps;
144 XErrorHandler old_handler;
145 const char *display_name = XDisplayName (NULL);
146
147 /* Don't use MIT-SHM on remote displays */
148 if (*display_name && *display_name != ':')
149 return false;
150
151 if (!XShmQueryVersion(fl_display, &major, &minor, &pixmaps))
152 return false;
153
154 shminfo = new XShmSegmentInfo;
155
156 xim = XShmCreateImage(fl_display, CopyFromParent, 32,
157 ZPixmap, 0, shminfo, width(), height());
158 if (!xim)
159 goto free_shminfo;
160
161 shminfo->shmid = shmget(IPC_PRIVATE,
162 xim->bytes_per_line * xim->height,
Pierre Ossmand71508b2017-03-29 13:28:55 +0200163 IPC_CREAT|0600);
Pierre Ossman403ac272017-01-02 17:00:41 +0100164 if (shminfo->shmid == -1)
165 goto free_xim;
166
167 shminfo->shmaddr = xim->data = (char*)shmat(shminfo->shmid, 0, 0);
168 shmctl(shminfo->shmid, IPC_RMID, 0); // to avoid memory leakage
169 if (shminfo->shmaddr == (char *)-1)
170 goto free_xim;
171
172 shminfo->readOnly = True;
173
174 // This is the only way we can detect that shared memory won't work
175 // (e.g. because we're accessing a remote X11 server)
176 caughtError = false;
177 old_handler = XSetErrorHandler(XShmAttachErrorHandler);
178
179 if (!XShmAttach(fl_display, shminfo)) {
180 XSetErrorHandler(old_handler);
181 goto free_shmaddr;
182 }
183
184 XSync(fl_display, False);
185
186 XSetErrorHandler(old_handler);
187
188 if (caughtError)
189 goto free_shmaddr;
190
191 vlog.debug("Using shared memory XImage");
192
193 return true;
194
195free_shmaddr:
196 shmdt(shminfo->shmaddr);
197
198free_xim:
199 XDestroyImage(xim);
200 xim = NULL;
201
202free_shminfo:
203 delete shminfo;
204 shminfo = NULL;
205
206 return 0;
207}
208
209#endif