1<?xml version="1.0" encoding="UTF-8" ?>
2<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
3                   "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd"
4[
5<!ENTITY % defs SYSTEM "defs.ent"> %defs;
6]>
7
8
9<!-- lifted from troff+ms+XMan by doclifter -->
10<book id="shm">
11
12<bookinfo>
13   <title>MIT-SHM(The MIT Shared Memory Extension)</title>
14   <subtitle>How the shared memory extension works</subtitle>
15   <authorgroup>
16      <author>
17         <firstname>Jonathan</firstname><surname>Corbet</surname>
18         <affiliation>
19            <orgname>National Center for Atmospheric Research</orgname>
20            <orgdiv>Atmospheric Technology Division</orgdiv>
21         </affiliation>
22      <email>corbet@ncar.ucar.edu</email>
23      </author>
24      <editor>
25         <firstname>Keith</firstname><surname>Packard</surname>
26         <affiliation><orgname>MIT X Consortium</orgname></affiliation>
27      </editor>
28   </authorgroup>
29   <releaseinfo>X Version 11, Release &fullrelvers;</releaseinfo>
30   <releaseinfo>Version 1.0</releaseinfo>
31   <copyright><year>1991</year><holder>X Consortium</holder></copyright>
32
33<legalnotice>
34<para>
35Permission is hereby granted, free of charge, to any person obtaining a copy
36of this software and associated documentation files (the "Software"), to deal
37in the Software without restriction, including without limitation the rights
38to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
39copies of the Software, and to permit persons to whom the Software is
40furnished to do so, subject to the following conditions:
41</para>
42<para>
43The above copyright notice and this permission notice shall be included in
44all copies or substantial portions of the Software.
45</para>
46<para>
47THE SOFTWARE IS PROVIDED &ldquo;AS IS&rdquo;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
48IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
49FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
50X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
51AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53</para>
54<para>
55Except as contained in this notice, the name of the X Consortium shall not be
56used in advertising or otherwise to promote the sale, use or other dealings
57in this Software without prior written authorization from the X Consortium.
58</para>
59<para>X Window System is a trademark of The OpenGroup.</para>
60</legalnotice>
61
62<abstract>
63<para>
64This document briefly describes how to use the MIT-SHM shared memory
65extension.  I have tried to make it accurate, but it would not surprise me
66if some errors remained.  If you find anything wrong, do let me know and I
67will incorporate the corrections.  Meanwhile, please take this document "as
68is" (eman improvement over what was there before, but certainly not the
69definitive word.)
70</para>
71</abstract>
72
73</bookinfo>
74
75<chapter id="REQUIREMENTS">
76<title>REQUIREMENTS</title>
77<para>
78The shared memory extension is provided only by some X servers.  To find out
79if your server supports the extension, use xdpyinfo(1).  In particular, to
80be able to use this extension, your system must provide the SYSV shared
81memory primitives.  There is not an mmap-based version of this extension.
82To use shared memory on Sun systems, you must have built your kernel with
83SYSV shared memory enabled -- which is not the default configuration.
84Additionally, the shared memory maximum size will need to be increased on
85both Sun and Digital systems; the defaults are far too small for any useful
86work.
87</para>
88</chapter>
89
90<chapter id="WHAT_IS_PROVIDED">
91<title>WHAT IS PROVIDED</title>
92
93<para>
94The basic capability provided is that of shared memory XImages.  This is
95essentially a version of the ximage interface where the actual image data
96is stored in a shared memory segment, and thus need not be moved through
97the Xlib interprocess communication channel.  For large images, use of this
98facility can result in some real performance increases.
99</para>
100
101<para>
102Additionally, some implementations provided shared memory pixmaps.  These
103are 2 dimensional arrays of pixels in a format specified by the X server,
104where the image data is stored in the shared memory segment.  Through use of
105shared memory pixmaps, it is possible to change the contents of these
106pixmaps without using any Xlib routines at all.  Shared memory pixmaps can
107only be supported when the X server can use regular virtual memory for
108pixmap data; if the pixmaps are stored in some magic graphics hardware, your
109application will not be able to share them with the server.  Xdpyinfo(1)
110doesn't print this particular nugget of information.
111</para>
112</chapter>
113
114<chapter id="HOW_TO_USE_THE_SHARED_MEMORY_EXTENSION">
115<title>HOW TO USE THE SHARED MEMORY EXTENSION</title>
116<para>
117Code which uses the shared memory extension must include a number of header
118files:
119</para>
120
121<literallayout class="monospaced">
122#include &lt;X11/Xlib.h&gt;          /* of course */
123#include &lt;sys/ipc.h&gt;
124#include &lt;sys/shm.h&gt;
125#include &lt;X11/extensions/XShm.h&gt;
126</literallayout>
127
128<para>
129Of course, if the system you are building on does not support shared
130memory, the file XShm.h may not be present.  You may want to make
131liberal use of #ifdefs.
132</para>
133
134<para>
135Any code which uses the shared memory extension should first check to see
136that the server provides the extension.  You could always be running over
137the net, or in some other environment where the extension will not work.
138To perform this check, call either
139</para>
140
141<funcsynopsis id='XShmQueryExtension'>
142<funcprototype>
143  <funcdef>Status <function>XShmQueryExtension</function></funcdef>
144    <paramdef>Display <parameter>*display</parameter></paramdef>
145</funcprototype>
146</funcsynopsis>
147
148<para>
149or
150</para>
151
152<funcsynopsis id='XShmQueryVersion'>
153<funcprototype>
154  <funcdef>Status <function>XShmQueryVersion</function></funcdef>
155    <paramdef>Display <parameter>*display</parameter></paramdef>
156    <paramdef>int <parameter>*major</parameter></paramdef>
157    <paramdef>int <parameter>*minor</parameter></paramdef>
158    <paramdef>Bool <parameter>*pixmaps</parameter></paramdef>
159</funcprototype>
160</funcsynopsis>
161
162<para>
163Where "display" is, of course, the display on which you are running.  If
164the shared memory extension may be used, the return value from either
165function will be True; otherwise your program should operate using
166conventional Xlib calls.  When the extension is available,
167\fCXShmQueryVersion\fP also returns "major" and "minor" which are the
168version numbers of the extension implementation, and "pixmaps" which is
169True iff shared memory pixmaps are supported.
170</para>
171</chapter>
172
173<chapter id="USE_OF_SHARED_MEMORY_XIMAGES">
174<title>USE OF SHARED MEMORY XIMAGES</title>
175<para>
176The basic sequence of operations for shared memory XImages is as follows:
177</para>
178
179<orderedlist>
180  <listitem>
181    <para>
182Create the shared memory XImage structure
183    </para>
184  </listitem>
185  <listitem>
186    <para>
187Create a shared memory segment to store the image data
188    </para>
189  </listitem>
190  <listitem>
191    <para>
192Inform the server about the shared memory segment
193    </para>
194  </listitem>
195  <listitem>
196    <para>
197Use the shared memory XImage, much like a normal one.
198    </para>
199  </listitem>
200</orderedlist>
201
202<para>
203To create a shared memory XImage, use:
204</para>
205
206<funcsynopsis id='XShmCreateImage'>
207<funcprototype>
208  <funcdef>XImage <function>*XShmCreateImage</function></funcdef>
209    <paramdef>Display <parameter>*display</parameter></paramdef>
210    <paramdef>Visual <parameter>*visual</parameter></paramdef>
211    <paramdef>unsigned int <parameter>depth</parameter></paramdef>
212    <paramdef>int <parameter>format</parameter></paramdef>
213    <paramdef>char <parameter>*data</parameter></paramdef>
214    <paramdef>XShmSegmentInfo <parameter>*shminfo</parameter></paramdef>
215    <paramdef>unsigned int <parameter>width</parameter></paramdef>
216    <paramdef>unsigned int <parameter>height</parameter></paramdef>
217</funcprototype>
218</funcsynopsis>
219
220<para>
221Most of the arguments are the same as for XCreateImage; I will not go
222through them here.  Note, however, that there are no "offset", "bitmap_pad",
223or "bytes_per_line" arguments.  These quantities will be defined by the
224server itself, and your code needs to abide by them.  Unless you have already
225allocated the shared memory segment (see below), you should pass in NULL for
226the "data" pointer.
227</para>
228
229<para>
230There is one additional argument: "shminfo", which is a pointer to a
231structure of type XShmSegmentInfo.  You must allocate one of these
232structures such that it will have a lifetime at least as long as that of
233the shared memory XImage.  There is no need to initialize this structure
234before the call to XShmCreateImage.
235</para>
236
237<para>
238The return value, if all goes well, will be an XImage structure, which you
239can use for the subsequent steps.
240</para>
241
242<para>
243The next step is to create the shared memory segment.  This is
244best done after the creation of the XImage, since you need to make use of
245the information in that XImage to know how much memory to allocate.  To
246create the segment, you need a call like:
247</para>
248
249
250<literallayout class="monospaced">
251shminfo.shmid = shmget (IPC_PRIVATE,
252          image-&gt;bytes_per_line * image-&gt;height, IPC_CREAT|0777);
253</literallayout>
254
255<para>
256(assuming that you have called your shared memory XImage "image").  You
257should, of course, follow the Rules and do error checking on all of these
258system calls.  Also, be sure to use the bytes_per_line field, not the width
259you used to create the XImage as they may well be different.
260</para>
261
262<para>
263Note that the shared memory ID returned by the system is stored in the
264shminfo structure.  The server will need that ID to attach itself to the
265segment.
266</para>
267
268<para>
269Also note that, on many systems for security reasons, the X server
270will only accept to attach to the shared memory segment if it's
271readable and writeable by "other". On systems where the X server is
272able to determine the uid of the X client over a local transport, the
273shared memory segment can be readable and writeable only by the uid of
274the client.
275</para>
276
277<para>
278Next, attach this shared memory segment to your process:
279</para>
280
281<para>
282shminfo.shmaddr = image-&gt;data = shmat (shminfo.shmid, 0, 0);
283</para>
284
285<para>
286The address returned by shmat should be stored in *both* the XImage
287structure and the shminfo structure.
288</para>
289
290<para>
291To finish filling in the shminfo structure, you need to decide how you want
292the server to attach to the shared memory segment, and set the "readOnly"
293field as follows.  Normally, you would code:
294</para>
295<para>
296shminfo.readOnly = False;
297</para>
298
299<para>
300If you set it to True, the server will not be able to write to this
301segment, and thus XShmGetImage calls will fail.
302</para>
303
304<para>
305Finally, tell the server to attach to your shared memory segment with:
306</para>
307
308<literallayout class="monospaced">
309Status XShmAttach (display, shminfo);
310</literallayout>
311
312<para>
313If all goes well, you will get a non-zero status back, and your XImage is
314ready for use.
315</para>
316
317<para>
318To write a shared memory XImage into an X drawable, use XShmPutImage:
319</para>
320
321<funcsynopsis id='XShmPutImage'>
322<funcprototype>
323  <funcdef>Status <function>XShmPutImage </function></funcdef>
324    <paramdef>Display <parameter>*display</parameter></paramdef>
325    <paramdef>Drawable <parameter>d</parameter></paramdef>
326    <paramdef>GC <parameter>gc</parameter></paramdef>
327    <paramdef>XImage <parameter>*image</parameter></paramdef>
328    <paramdef>int <parameter>src_x</parameter></paramdef>
329    <paramdef>int <parameter>src_y</parameter></paramdef>
330    <paramdef>int <parameter>dest_x</parameter></paramdef>
331    <paramdef>int <parameter>dest_y</parameter></paramdef>
332    <paramdef>unsigned int <parameter>width</parameter></paramdef>
333    <paramdef>unsigned int <parameter>height</parameter></paramdef>
334    <paramdef>bool <parameter>send_event</parameter></paramdef>
335</funcprototype>
336</funcsynopsis>
337
338<para>
339The interface is identical to that of XPutImage, so I will spare my fingers
340and not repeat that documentation here.  There is one additional parameter,
341however, called "send_event".  If this parameter is passed as True, the
342server will generate a "completion" event when the image write is complete;
343thus your program can know when it is safe to begin manipulating the shared
344memory segment again.
345</para>
346
347<para>
348The completion event has type XShmCompletionEvent, which is defined as the
349following:
350</para>
351
352<literallayout class="monospaced">
353typedef struct {
354    int type;              /* of event */
355    unsigned long serial;  /* # of last request processed */
356    Bool send_event;       /* true if came from a SendEvent request */
357    Display *display;      /* Display the event was read from */
358    Drawable drawable;     /* drawable of request */
359    int major_code;        /* ShmReqCode */
360    int minor_code;        /* X_ShmPutImage */
361    ShmSeg shmseg;         /* the ShmSeg used in the request */
362    unsigned long offset;  /* the offset into ShmSeg used */
363} XShmCompletionEvent;
364</literallayout>
365
366<para>
367The event type value that will be used can be determined at run time with a
368line of the form:
369</para>
370
371<para>
372int CompletionType = XShmGetEventBase (display) + ShmCompletion;
373</para>
374
375<para>
376If you modify the shared memory segment before the arrival of the
377completion event, the results you see on the screen may be inconsistent.
378</para>
379
380<para>
381To read image data into a shared memory XImage, use the following:
382</para>
383
384<funcsynopsis id='XShmGetImage'>
385<funcprototype>
386  <funcdef>Status <function>XShmGetImage </function></funcdef>
387    <paramdef>Display <parameter>*display</parameter></paramdef>
388    <paramdef>Drawable <parameter>d</parameter></paramdef>
389    <paramdef>XImage <parameter>*image</parameter></paramdef>
390    <paramdef>int <parameter>x</parameter></paramdef>
391    <paramdef>int <parameter>y</parameter></paramdef>
392    <paramdef>unsigned long <parameter>plane_mask</parameter></paramdef>
393</funcprototype>
394</funcsynopsis>
395
396<para>
397Where "display" is the display of interest, "d" is the source drawable,
398"image" is the destination XImage, "x" and "y" are the offsets within
399"d", and "plane_mask" defines which planes are to be read.
400</para>
401
402<para>
403To destroy a shared memory XImage, you should first instruct the server to
404detach from it, then destroy the segment itself, as follows:
405</para>
406
407<literallayout class="monospaced">
408XShmDetach (display, shminfo);
409XDestroyImage (image);
410shmdt (shminfo.shmaddr);
411shmctl (shminfo.shmid, IPC_RMID, 0);
412</literallayout>
413
414</chapter>
415
416<chapter id="USE_OF_SHARED_MEMORY_PIXMAPS">
417<title>USE OF SHARED MEMORY PIXMAPS</title>
418<para>
419Unlike X images, for which any image format is usable, the shared memory
420extension supports only a single format (i.e. XYPixmap or ZPixmap) for the
421data stored in a shared memory pixmap.  This format is independent of the
422depth of the image (for 1-bit pixmaps it doesn't really matter what this
423format is) and independent of the screen.  Use XShmPixmapFormat to get the
424format for the server:
425</para>
426
427<funcsynopsis id='XShmPixmapFormat'>
428<funcprototype>
429  <funcdef>int <function>XShmPixmapFormat</function></funcdef>
430    <paramdef>Display <parameter>*display</parameter></paramdef>
431</funcprototype>
432</funcsynopsis>
433
434<para>
435If your application can deal with the server pixmap data format (including
436bits-per-pixel et al.), create a shared memory segment and "shminfo"
437structure in exactly the same way as is listed above for shared memory
438XImages.  While it is, not strictly necessary to create an XImage first,
439doing so incurs little overhead and will give you an appropriate
440bytes_per_line value to use.
441</para>
442
443<para>
444Once you have your shminfo structure filled in, simply call:
445</para>
446
447<funcsynopsis id='XShmCreatePixmap'>
448<funcprototype>
449  <funcdef>Pixmap <function>XShmCreatePixmap</function></funcdef>
450    <paramdef>Display <parameter>*display</parameter></paramdef>
451    <paramdef>Drawable <parameter>d</parameter></paramdef>
452    <paramdef>char <parameter>*data</parameter></paramdef>
453    <paramdef>XShmSegmentInfo <parameter>*shminfo</parameter></paramdef>
454    <paramdef>unsigned int <parameter>width</parameter></paramdef>
455    <paramdef>unsigned int <parameter>height</parameter></paramdef>
456    <paramdef>unsigned int <parameter>depth</parameter></paramdef>
457</funcprototype>
458</funcsynopsis>
459
460<para>
461The arguments are all the same as for XCreatePixmap, with two additions:
462"data" and "shminfo".  The second of the two is the same old shminfo
463structure that has been used before.  The first is the pointer to the shared
464memory segment, and should be the same as the shminfo.shmaddr field.  I am
465not sure why this is a separate parameter.
466</para>
467
468<para>
469If everything works, you will get back a pixmap, which you can manipulate in
470all of the usual ways, with the added bonus of being able to tweak its
471contents directly through the shared memory segment.  Shared memory pixmaps
472are destroyed in the usual manner with XFreePixmap, though you should detach
473and destroy the shared memory segment itself as shown above.
474</para>
475</chapter>
476</book>
477