blob: 4802ba46836ecbd7a1aae29390083c2684b1dea2 [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) :
37 FullFramePixelBuffer(rfb::PixelFormat(32, 24, false, true,
38 255, 255, 255, 16, 8, 0),
39 width, height, 0, stride),
40 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);
61#else
62 FullFramePixelBuffer::data = (rdr::U8*)Surface::data;
63 stride = width;
64#endif
65}
66
67PlatformPixelBuffer::~PlatformPixelBuffer()
68{
69#if !defined(WIN32) && !defined(__APPLE__)
70 if (shminfo) {
71 vlog.debug("Freeing shared memory XImage");
72 shmdt(shminfo->shmaddr);
73 shmctl(shminfo->shmid, IPC_RMID, 0);
74 delete shminfo;
75 shminfo = NULL;
76 }
77
78 // XDestroyImage() will free(xim->data) if appropriate
79 if (xim)
80 XDestroyImage(xim);
81 xim = NULL;
82#endif
Pierre Ossmanac13abe2014-02-07 14:46:26 +010083}
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020084
85void PlatformPixelBuffer::commitBufferRW(const rfb::Rect& r)
86{
87 FullFramePixelBuffer::commitBufferRW(r);
Pierre Ossman0b5a06b2015-11-13 14:09:27 +010088 mutex.lock();
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020089 damage.assign_union(rfb::Region(r));
Pierre Ossman0b5a06b2015-11-13 14:09:27 +010090 mutex.unlock();
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020091}
92
93rfb::Rect PlatformPixelBuffer::getDamage(void)
94{
95 rfb::Rect r;
96
Pierre Ossman0b5a06b2015-11-13 14:09:27 +010097 mutex.lock();
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020098 r = damage.get_bounding_rect();
99 damage.clear();
Pierre Ossman0b5a06b2015-11-13 14:09:27 +0100100 mutex.unlock();
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200101
Pierre Ossman403ac272017-01-02 17:00:41 +0100102#if !defined(WIN32) && !defined(__APPLE__)
103 GC gc;
104
105 gc = XCreateGC(fl_display, pixmap, 0, NULL);
106 if (shminfo) {
107 XShmPutImage(fl_display, pixmap, gc, xim,
108 r.tl.x, r.tl.y, r.tl.x, r.tl.y,
109 r.width(), r.height(), False);
110 // Need to make sure the X server has finished reading the
111 // shared memory before we return
112 XSync(fl_display, False);
113 } else {
114 XPutImage(fl_display, pixmap, gc, xim,
115 r.tl.x, r.tl.y, r.tl.x, r.tl.y, r.width(), r.height());
116 }
117 XFreeGC(fl_display, gc);
118#endif
119
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200120 return r;
121}
Pierre Ossman403ac272017-01-02 17:00:41 +0100122
123#if !defined(WIN32) && !defined(__APPLE__)
124
125static bool caughtError;
126
127static int XShmAttachErrorHandler(Display *dpy, XErrorEvent *error)
128{
129 caughtError = true;
130 return 0;
131}
132
133bool PlatformPixelBuffer::setupShm()
134{
135 int major, minor;
136 Bool pixmaps;
137 XErrorHandler old_handler;
138 const char *display_name = XDisplayName (NULL);
139
140 /* Don't use MIT-SHM on remote displays */
141 if (*display_name && *display_name != ':')
142 return false;
143
144 if (!XShmQueryVersion(fl_display, &major, &minor, &pixmaps))
145 return false;
146
147 shminfo = new XShmSegmentInfo;
148
149 xim = XShmCreateImage(fl_display, CopyFromParent, 32,
150 ZPixmap, 0, shminfo, width(), height());
151 if (!xim)
152 goto free_shminfo;
153
154 shminfo->shmid = shmget(IPC_PRIVATE,
155 xim->bytes_per_line * xim->height,
156 IPC_CREAT|0777);
157 if (shminfo->shmid == -1)
158 goto free_xim;
159
160 shminfo->shmaddr = xim->data = (char*)shmat(shminfo->shmid, 0, 0);
161 shmctl(shminfo->shmid, IPC_RMID, 0); // to avoid memory leakage
162 if (shminfo->shmaddr == (char *)-1)
163 goto free_xim;
164
165 shminfo->readOnly = True;
166
167 // This is the only way we can detect that shared memory won't work
168 // (e.g. because we're accessing a remote X11 server)
169 caughtError = false;
170 old_handler = XSetErrorHandler(XShmAttachErrorHandler);
171
172 if (!XShmAttach(fl_display, shminfo)) {
173 XSetErrorHandler(old_handler);
174 goto free_shmaddr;
175 }
176
177 XSync(fl_display, False);
178
179 XSetErrorHandler(old_handler);
180
181 if (caughtError)
182 goto free_shmaddr;
183
184 vlog.debug("Using shared memory XImage");
185
186 return true;
187
188free_shmaddr:
189 shmdt(shminfo->shmaddr);
190
191free_xim:
192 XDestroyImage(xim);
193 xim = NULL;
194
195free_shminfo:
196 delete shminfo;
197 shminfo = NULL;
198
199 return 0;
200}
201
202#endif