home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 August - Disc 2 / chip_20018102_hu.iso / linux / X-4.1.0 / doc / mit-shm.txt < prev    next >
Text File  |  2001-06-27  |  13KB  |  529 lines

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