f7e44873266ef9ff37119ceae2af722b4f5786c5
[xscreensaver] / hacks / glx / grab-ximage.c
1 /* grab-ximage.c --- grab the screen to an XImage for use with OpenGL.
2  * xscreensaver, Copyright (c) 2001, 2003, 2005 Jamie Zawinski <jwz@jwz.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22 #include <GL/gl.h>      /* only for GLfloat */
23 #include <GL/glu.h>     /* for gluBuild2DMipmaps */
24
25 #include "grabscreen.h"
26 #include "visual.h"
27
28 /* If REFORMAT_IMAGE_DATA is defined, then we convert Pixmaps to textures
29    like this:
30
31      - get Pixmap as an XImage in whatever form the server hands us;
32      - convert that XImage to 32-bit RGBA in client-local endianness;
33      - make the texture using RGBA, UNSIGNED_BYTE.
34
35    If undefined, we do this:
36
37      - get Pixmap as an XImage in whatever form the server hands us;
38      - figure out what OpenGL texture packing parameters correspond to
39        the image data that the server sent us and use that, e.g.,
40        BGRA, INT_8_8_8_8_REV.
41
42    You might expect the second method to be faster, since we're not making
43    a second copy of the data and iterating each pixel before we hand it
44    to GL.  But, you'd be wrong.  The first method is almost 6x faster.
45    I guess GL is reformatting it *again*, and doing it very inefficiently!
46 */
47 #define REFORMAT_IMAGE_DATA
48
49
50 #ifdef HAVE_XSHM_EXTENSION
51 # include "resources.h"
52 # include "xshm.h"
53 #endif /* HAVE_XSHM_EXTENSION */
54
55 extern char *progname;
56
57 #include <X11/Xutil.h>
58 #include <sys/time.h>
59
60 #undef MAX
61 #define MAX(a,b) ((a)>(b)?(a):(b))
62
63 #undef countof
64 #define countof(x) (sizeof((x))/sizeof((*x)))
65
66
67 static int debug_p = 0;
68
69 static Bool
70 bigendian (void)
71 {
72   union { int i; char c[sizeof(int)]; } u;
73   u.i = 1;
74   return !u.c[0];
75 }
76
77
78 #ifdef REFORMAT_IMAGE_DATA
79
80 /* Given a bitmask, returns the position and width of the field.
81  */
82 static void
83 decode_mask (unsigned int mask, unsigned int *pos_ret, unsigned int *size_ret)
84 {
85   int i;
86   for (i = 0; i < 32; i++)
87     if (mask & (1L << i))
88       {
89         int j = 0;
90         *pos_ret = i;
91         for (; i < 32; i++, j++)
92           if (! (mask & (1L << i)))
93             break;
94         *size_ret = j;
95         return;
96       }
97 }
98
99
100 /* Given a value and a field-width, expands the field to fill out 8 bits.
101  */
102 static unsigned char
103 spread_bits (unsigned char value, unsigned char width)
104 {
105   switch (width)
106     {
107     case 8: return value;
108     case 7: return (value << 1) | (value >> 6);
109     case 6: return (value << 2) | (value >> 4);
110     case 5: return (value << 3) | (value >> 2);
111     case 4: return (value << 4) | (value);
112     case 3: return (value << 5) | (value << 2) | (value >> 2);
113     case 2: return (value << 6) | (value << 4) | (value);
114     default: abort(); break;
115     }
116 }
117
118
119 static XImage *
120 convert_ximage_to_rgba32 (Screen *screen, XImage *image)
121 {
122   Display *dpy = DisplayOfScreen (screen);
123   Visual *visual = DefaultVisualOfScreen (screen);
124
125   int x, y;
126   unsigned int crpos=0, cgpos=0, cbpos=0, capos=0; /* bitfield positions */
127   unsigned int srpos=0, sgpos=0, sbpos=0;
128   unsigned int srmsk=0, sgmsk=0, sbmsk=0;
129   unsigned int srsiz=0, sgsiz=0, sbsiz=0;
130   int i;
131   XColor *colors = 0;
132   unsigned char spread_map[3][256];
133
134   /* Note: height+2 in "to" to be to work around an array bounds overrun
135      in gluBuild2DMipmaps / gluScaleImage.
136    */
137   XImage *from = image;
138   XImage *to = XCreateImage (dpy, visual, 32,  /* depth */
139                              ZPixmap, 0, 0, from->width, from->height + 2,
140                              32, /* bitmap pad */
141                              0);
142   to->data = (char *) calloc (to->height, to->bytes_per_line);
143
144   if (visual_class (screen, visual) == PseudoColor ||
145       visual_class (screen, visual) == GrayScale)
146     {
147       Colormap cmap = DefaultColormapOfScreen (screen);
148       int ncolors = visual_cells (screen, visual);
149       int i;
150       colors = (XColor *) calloc (sizeof (*colors), ncolors+1);
151       for (i = 0; i < ncolors; i++)
152         colors[i].pixel = i;
153       XQueryColors (dpy, cmap, colors, ncolors);
154     }
155
156   if (colors == 0)  /* truecolor */
157     {
158       srmsk = to->red_mask;
159       sgmsk = to->green_mask;
160       sbmsk = to->blue_mask;
161
162       decode_mask (srmsk, &srpos, &srsiz);
163       decode_mask (sgmsk, &sgpos, &sgsiz);
164       decode_mask (sbmsk, &sbpos, &sbsiz);
165     }
166
167   /* Pack things in "RGBA" order in client endianness. */
168   if (bigendian())
169     crpos = 24, cgpos = 16, cbpos =  8, capos =  0;
170   else
171     crpos =  0, cgpos =  8, cbpos = 16, capos = 24;
172
173   if (colors == 0)  /* truecolor */
174     {
175       for (i = 0; i < 256; i++)
176         {
177           spread_map[0][i] = spread_bits (i, srsiz);
178           spread_map[1][i] = spread_bits (i, sgsiz);
179           spread_map[2][i] = spread_bits (i, sbsiz);
180         }
181     }
182
183   for (y = 0; y < from->height; y++)
184     for (x = 0; x < from->width; x++)
185       {
186         unsigned long sp = XGetPixel (from, x, y);
187         unsigned char sr, sg, sb;
188         unsigned long cp;
189
190         if (colors)
191           {
192             sr = colors[sp].red   & 0xFF;
193             sg = colors[sp].green & 0xFF;
194             sb = colors[sp].blue  & 0xFF;
195           }
196         else
197           {
198             sr = (sp & srmsk) >> srpos;
199             sg = (sp & sgmsk) >> sgpos;
200             sb = (sp & sbmsk) >> sbpos;
201
202             sr = spread_map[0][sr];
203             sg = spread_map[1][sg];
204             sb = spread_map[2][sb];
205           }
206
207         cp = ((sr << crpos) |
208               (sg << cgpos) |
209               (sb << cbpos) |
210               (0xFF << capos));
211
212         XPutPixel (to, x, y, cp);
213       }
214
215   if (colors) free (colors);
216
217   return to;
218 }
219
220 #endif /* REFORMAT_IMAGE_DATA */
221
222 /* Shrinks the XImage by a factor of two.
223    We use this when mipmapping fails on large textures.
224  */
225 static void
226 halve_image (XImage *ximage, XRectangle *geom)
227 {
228   int w2 = ximage->width/2;
229   int h2 = ximage->height/2;
230   int x, y;
231   XImage *ximage2;
232
233   if (w2 <= 32 || h2 <= 32)   /* let's not go crazy here, man. */
234     return;
235
236   if (debug_p)
237     fprintf (stderr, "%s: shrinking image %dx%d -> %dx%d\n",
238              progname, ximage->width, ximage->height, w2, h2);
239
240   ximage2 = (XImage *) calloc (1, sizeof (*ximage2));
241   *ximage2 = *ximage;
242   ximage2->width = w2;
243   ximage2->height = h2;
244   ximage2->bytes_per_line = 0;
245   ximage2->data = 0;
246   XInitImage (ximage2);
247
248   ximage2->data = (char *) calloc (h2, ximage2->bytes_per_line);
249   if (!ximage2->data)
250     {
251       fprintf (stderr, "%s: out of memory (scaling %dx%d image to %dx%d)\n",
252                progname, ximage->width, ximage->height, w2, h2);
253       exit (1);
254     }
255
256   for (y = 0; y < h2; y++)
257     for (x = 0; x < w2; x++)
258       XPutPixel (ximage2, x, y, XGetPixel (ximage, x*2, y*2));
259
260   free (ximage->data);
261   *ximage = *ximage2;
262   ximage2->data = 0;
263   XFree (ximage2);
264
265   if (geom)
266     {
267       geom->x /= 2;
268       geom->y /= 2;
269       geom->width  /= 2;
270       geom->height /= 2;
271     }
272 }
273
274
275 #ifdef REFORMAT_IMAGE_DATA
276
277 /* Pulls the Pixmap bits from the server and returns an XImage
278    in some format acceptable to OpenGL.
279  */
280 static XImage *
281 pixmap_to_gl_ximage (Screen *screen, Window window, Pixmap pixmap)
282 {
283   Display *dpy = DisplayOfScreen (screen);
284   Visual *visual = DefaultVisualOfScreen (screen);
285   unsigned int width, height, depth;
286
287 # ifdef HAVE_XSHM_EXTENSION
288   Bool use_shm = get_boolean_resource ("useSHM", "Boolean");
289   XShmSegmentInfo shm_info;
290 # endif /* HAVE_XSHM_EXTENSION */
291
292   XImage *server_ximage = 0;
293   XImage *client_ximage = 0;
294
295   {
296     Window root;
297     int x, y;
298     unsigned int bw;
299     XGetGeometry (dpy, pixmap, &root, &x, &y, &width, &height, &bw, &depth);
300   }
301
302   /* Convert the server-side Pixmap to a client-side GL-ordered XImage.
303    */
304 # ifdef HAVE_XSHM_EXTENSION
305   if (use_shm)
306     {
307       server_ximage = create_xshm_image (dpy, visual, depth,
308                                          ZPixmap, 0, &shm_info,
309                                          width, height);
310       if (server_ximage)
311         XShmGetImage (dpy, pixmap, server_ximage, 0, 0, ~0L);
312       else
313         use_shm = False;
314     }
315 # endif /* HAVE_XSHM_EXTENSION */
316
317   if (!server_ximage)
318     server_ximage = XGetImage (dpy, pixmap, 0, 0, width, height, ~0L, ZPixmap);
319
320   client_ximage = convert_ximage_to_rgba32 (screen, server_ximage);
321
322 # ifdef HAVE_XSHM_EXTENSION
323   if (use_shm)
324     destroy_xshm_image (dpy, server_ximage, &shm_info);
325   else
326 # endif /* HAVE_XSHM_EXTENSION */
327     XDestroyImage (server_ximage);
328
329   return client_ximage;
330 }
331
332
333 # else /* ! REFORMAT_IMAGE_DATA */
334
335 typedef struct {
336   unsigned int depth, red_mask, green_mask, blue_mask;  /* when this... */
337   GLint type, format;                                   /* ...use this. */
338 } conversion_table;
339
340 /* Abbreviate these so that the table entries all fit on one line...
341  */
342 #define BYTE               GL_UNSIGNED_BYTE
343 #define BYTE_2_3_3_REV     GL_UNSIGNED_BYTE_2_3_3_REV
344 #define BYTE_3_3_2         GL_UNSIGNED_BYTE_3_3_2
345 #define INT_10_10_10_2     GL_UNSIGNED_INT_10_10_10_2
346 #define INT_2_10_10_10_REV GL_UNSIGNED_INT_2_10_10_10_REV
347 #define INT_8_8_8_8        GL_UNSIGNED_INT_8_8_8_8
348 #define INT_8_8_8_8_REV    GL_UNSIGNED_INT_8_8_8_8_REV
349 #define SHORT_1_5_5_5_REV  GL_UNSIGNED_SHORT_1_5_5_5_REV
350 #define SHORT_4_4_4_4      GL_UNSIGNED_SHORT_4_4_4_4
351 #define SHORT_4_4_4_4_REV  GL_UNSIGNED_SHORT_4_4_4_4_REV
352 #define SHORT_5_5_5_1      GL_UNSIGNED_SHORT_5_5_5_1
353 #define SHORT_5_6_5        GL_UNSIGNED_SHORT_5_6_5
354 #define SHORT_5_6_5_REV    GL_UNSIGNED_SHORT_5_6_5_REV
355
356 static conversion_table ctable[] = {
357   { 8,  0x000000E0, 0x0000001C, 0x00000003, BYTE_3_3_2,         GL_RGB      },
358   { 8,  0x00000007, 0x00000038, 0x000000C0, BYTE_2_3_3_REV,     GL_RGB      },
359   { 16, 0x0000F800, 0x000007E0, 0x0000001F, SHORT_5_6_5,        GL_RGB      },
360   { 16, 0x0000001F, 0x000007E0, 0x0000F800, SHORT_5_6_5_REV,    GL_RGB      },
361   { 16, 0x0000F000, 0x00000F00, 0x000000F0, SHORT_4_4_4_4,      GL_RGBA     },
362   { 16, 0x000000F0, 0x00000F00, 0x0000F000, SHORT_4_4_4_4,      GL_BGRA     },
363   { 16, 0x0000000F, 0x000000F0, 0x00000F00, SHORT_4_4_4_4,      GL_ABGR_EXT },
364   { 16, 0x0000000F, 0x000000F0, 0x00000F00, SHORT_4_4_4_4_REV,  GL_RGBA     },
365   { 16, 0x00000F00, 0x000000F0, 0x0000000F, SHORT_4_4_4_4_REV,  GL_BGRA     },
366   { 16, 0x0000F800, 0x000007C0, 0x0000003E, SHORT_5_5_5_1,      GL_RGBA     },
367   { 16, 0x0000003E, 0x000007C0, 0x0000F800, SHORT_5_5_5_1,      GL_BGRA     },
368   { 16, 0x00000001, 0x0000003E, 0x000007C0, SHORT_5_5_5_1,      GL_ABGR_EXT },
369   { 16, 0x0000001F, 0x000003E0, 0x00007C00, SHORT_1_5_5_5_REV,  GL_RGBA     },
370   { 16, 0x00007C00, 0x000003E0, 0x0000001F, SHORT_1_5_5_5_REV,  GL_BGRA     },
371   { 32, 0xFF000000, 0x00FF0000, 0x0000FF00, INT_8_8_8_8,        GL_RGBA     },
372   { 32, 0x0000FF00, 0x00FF0000, 0xFF000000, INT_8_8_8_8,        GL_BGRA     },
373   { 32, 0x000000FF, 0x0000FF00, 0x00FF0000, INT_8_8_8_8,        GL_ABGR_EXT },
374   { 32, 0x000000FF, 0x0000FF00, 0x00FF0000, INT_8_8_8_8_REV,    GL_RGBA     },
375   { 32, 0x00FF0000, 0x0000FF00, 0x000000FF, INT_8_8_8_8_REV,    GL_BGRA     },
376   { 32, 0xFFC00000, 0x003FF000, 0x00000FFC, INT_10_10_10_2,     GL_RGBA     },
377   { 32, 0x00000FFC, 0x003FF000, 0xFFC00000, INT_10_10_10_2,     GL_BGRA     },
378   { 32, 0x00000003, 0x00000FFC, 0x003FF000, INT_10_10_10_2,     GL_ABGR_EXT },
379   { 32, 0x000003FF, 0x000FFC00, 0x3FF00000, INT_2_10_10_10_REV, GL_RGBA     },
380   { 32, 0x3FF00000, 0x000FFC00, 0x000003FF, INT_2_10_10_10_REV, GL_BGRA     },
381   { 24, 0x000000FF, 0x0000FF00, 0x00FF0000, BYTE,               GL_RGB      },
382   { 24, 0x00FF0000, 0x0000FF00, 0x000000FF, BYTE,               GL_BGR      },
383 };
384
385
386 /* Given an XImage, returns the GL settings to use its data as a texture.
387  */
388 static void
389 gl_settings_for_ximage (XImage *image,
390                         GLint *type_ret, GLint *format_ret, GLint *swap_ret)
391 {
392   int i;
393   for (i = 0; i < countof(ctable); ++i)
394     {
395       if (image->bits_per_pixel == ctable[i].depth &&
396           image->red_mask       == ctable[i].red_mask &&
397           image->green_mask     == ctable[i].green_mask &&
398           image->blue_mask      == ctable[i].blue_mask)
399         {
400           *type_ret   = ctable[i].type;
401           *format_ret = ctable[i].format;
402
403           if (image->bits_per_pixel == 24)
404             {
405               /* don't know how to test this... */
406               *type_ret = (ctable[i].type == GL_RGB) ? GL_BGR : GL_RGB;
407               *swap_ret = 0;
408             }
409           else
410             {
411               *swap_ret = !!(image->byte_order == MSBFirst) ^ !!bigendian();
412             }
413
414           if (debug_p)
415             {
416               fprintf (stderr, "%s: using %s %s %d for %d %08lX %08lX %08lX\n",
417                        progname,
418                        (*format_ret == GL_RGB      ? "RGB" :
419                         *format_ret == GL_BGR      ? "BGR" :
420                         *format_ret == GL_RGBA     ? "RGBA" :
421                         *format_ret == GL_BGRA     ? "BGRA" :
422                         *format_ret == GL_ABGR_EXT ? "ABGR_EXT" :
423                         "???"),
424                        (*type_ret == BYTE               ? "BYTE" :
425                         *type_ret == BYTE_3_3_2         ? "BYTE_3_3_2" :
426                         *type_ret == BYTE_2_3_3_REV     ? "BYTE_2_3_3_REV" :
427                         *type_ret == INT_8_8_8_8        ? "INT_8_8_8_8" :
428                         *type_ret == INT_8_8_8_8_REV    ? "INT_8_8_8_8_REV" :
429                         *type_ret == INT_10_10_10_2     ? "INT_10_10_10_2" :
430                         *type_ret == INT_2_10_10_10_REV ? "INT_2_10_10_10_REV":
431                         *type_ret == SHORT_4_4_4_4      ? "SHORT_4_4_4_4" :
432                         *type_ret == SHORT_4_4_4_4_REV  ? "SHORT_4_4_4_4_REV" :
433                         *type_ret == SHORT_5_5_5_1      ? "SHORT_5_5_5_1" :
434                         *type_ret == SHORT_1_5_5_5_REV  ? "SHORT_1_5_5_5_REV" :
435                         *type_ret == SHORT_5_6_5        ? "SHORT_5_6_5" :
436                         *type_ret == SHORT_5_6_5_REV    ? "SHORT_5_6_5_REV" :
437                         "???"),
438                        *swap_ret,
439                        image->bits_per_pixel,
440                        image->red_mask, image->green_mask, image->blue_mask);
441             }
442
443           return;
444         }
445     }
446
447   /* Unknown RGB fields? */
448   abort();
449 }
450
451 #endif /* ! REFORMAT_IMAGE_DATA */
452
453 typedef struct {
454   Pixmap pixmap;
455   int pix_width, pix_height, pix_depth;
456   int texid;
457   Bool mipmap_p;
458   double load_time;
459
460   /* Used in async mode
461    */
462   void (*callback) (const char *filename, XRectangle *geometry,
463                     int iw, int ih, int tw, int th,
464                     void *closure);
465   void *closure;
466
467   /* Used in sync mode
468    */
469   char **filename_return;
470   XRectangle *geometry_return;
471   int *image_width_return;
472   int *image_height_return;
473   int *texture_width_return;
474   int *texture_height_return;
475
476 } img_closure;
477
478
479 /* Returns the current time in seconds as a double.
480  */
481 static double
482 double_time (void)
483 {
484   struct timeval now;
485 # ifdef GETTIMEOFDAY_TWO_ARGS
486   struct timezone tzp;
487   gettimeofday(&now, &tzp);
488 # else
489   gettimeofday(&now);
490 # endif
491
492   return (now.tv_sec + ((double) now.tv_usec * 0.000001));
493 }
494
495
496 /* return the next larger power of 2. */
497 static int
498 to_pow2 (int i)
499 {
500   static unsigned int pow2[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
501                                  2048, 4096, 8192, 16384, 32768, 65536 };
502   int j;
503   for (j = 0; j < countof(pow2); j++)
504     if (pow2[j] >= i) return pow2[j];
505   abort();  /* too big! */
506 }
507
508
509 /* Loads the given XImage into GL's texture memory.
510    The image may be of any size.
511    If mipmap_p is true, then make mipmaps instead of just a single texture.
512    Writes to stderr and returns False on error.
513  */
514 static Bool
515 ximage_to_texture (XImage *ximage,
516                    GLint type, GLint format,
517                    int *width_return,
518                    int *height_return,
519                    XRectangle *geometry,
520                    Bool mipmap_p)
521 {
522   int max_reduction = 7;
523   int err_count = 0;
524   GLenum err = 0;
525   int orig_width = ximage->width;
526   int orig_height = ximage->height;
527   int tex_width = 0;
528   int tex_height = 0;
529
530  AGAIN:
531
532   if (mipmap_p)
533     {
534       /* gluBuild2DMipmaps doesn't require textures to be a power of 2. */
535       tex_width  = ximage->width;
536       tex_height = ximage->height;
537
538       if (debug_p)
539         fprintf (stderr, "%s: mipmap %d x %d\n",
540                  progname, ximage->width, ximage->height);
541
542       gluBuild2DMipmaps (GL_TEXTURE_2D, 3, ximage->width, ximage->height,
543                          format, type, ximage->data);
544       err = glGetError();
545     }
546   else
547     {
548       /* glTexImage2D() requires the texture sizes to be powers of 2.
549          So first, create a texture of that size (but don't write any
550          data into it.)
551        */
552       tex_width  = to_pow2 (ximage->width);
553       tex_height = to_pow2 (ximage->height);
554
555       if (debug_p)
556         fprintf (stderr, "%s: texture %d x %d (%d x %d)\n",
557                  progname, ximage->width, ximage->height,
558                  tex_width, tex_height);
559
560       glTexImage2D (GL_TEXTURE_2D, 0, 3, tex_width, tex_height, 0,
561                     format, type, 0);
562       err = glGetError();
563
564       /* Now load our non-power-of-2 image data into the existing texture. */
565       if (!err)
566         {
567           glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0,
568                            ximage->width, ximage->height,
569                            GL_RGBA, GL_UNSIGNED_BYTE, ximage->data);
570           err = glGetError();
571         }
572     }
573
574   if (err)
575     {
576       char buf[100];
577       const char *s = (char *) gluErrorString (err);
578
579       if (!s || !*s)
580         {
581           sprintf (buf, "unknown error %d", err);
582           s = buf;
583         }
584
585       while (glGetError() != GL_NO_ERROR)
586         ;  /* clear any lingering errors */
587
588       if (++err_count > max_reduction)
589         {
590           fprintf (stderr,
591                    "\n"
592                    "%s: %dx%d texture failed, even after reducing to %dx%d:\n"
593                    "%s: The error was: \"%s\".\n"
594                    "%s: probably this means "
595                    "\"your video card is worthless and weak\"?\n\n",
596                    progname, orig_width, orig_height,
597                    ximage->width, ximage->height,
598                    progname, s,
599                    progname);
600           return False;
601         }
602       else
603         {
604           if (debug_p)
605             fprintf (stderr, "%s: mipmap error (%dx%d): %s\n",
606                      progname, ximage->width, ximage->height, s);
607           halve_image (ximage, geometry);
608           goto AGAIN;
609         }
610     }
611
612   if (width_return)  *width_return  = tex_width;
613   if (height_return) *height_return = tex_height;
614   return True;
615 }
616
617
618 static void screen_to_texture_async_cb (Screen *screen,
619                                         Window window, Drawable drawable,
620                                         const char *name, XRectangle *geometry,
621                                         void *closure);
622
623
624 /* Grabs an image of the desktop (or another random image file) and
625    loads tht image into GL's texture memory.
626    Writes to stderr and returns False on error.
627  */
628 Bool
629 screen_to_texture (Screen *screen, Window window,
630                    int desired_width, int desired_height,
631                    Bool mipmap_p,
632                    char **filename_return,
633                    XRectangle *geometry_return,
634                    int *image_width_return,
635                    int *image_height_return,
636                    int *texture_width_return,
637                    int *texture_height_return)
638 {
639   Display *dpy = DisplayOfScreen (screen);
640   img_closure *data = (img_closure *) calloc (1, sizeof(*data));
641   XWindowAttributes xgwa;
642   char *filename = 0;
643   XRectangle geom = { 0, 0, 0, 0 };
644   int wret;
645
646   if (! image_width_return)
647     image_width_return = &wret;
648
649   if (debug_p)
650     data->load_time = double_time();
651
652   data->texid     = -1;
653   data->mipmap_p  = mipmap_p;
654   data->filename_return       = filename_return;
655   data->geometry_return       = geometry_return;
656   data->image_width_return    = image_width_return;
657   data->image_height_return   = image_height_return;
658   data->texture_width_return  = texture_width_return;
659   data->texture_height_return = texture_height_return;
660
661   XGetWindowAttributes (dpy, window, &xgwa);
662   data->pix_width  = xgwa.width;
663   data->pix_height = xgwa.height;
664   data->pix_depth  = xgwa.depth;
665
666   if (desired_width  && desired_width  < xgwa.width)
667     data->pix_width  = desired_width;
668   if (desired_height && desired_height < xgwa.height)
669     data->pix_height = desired_height;
670
671   data->pixmap = XCreatePixmap (dpy, window, data->pix_width, data->pix_height,
672                                 data->pix_depth);
673   load_random_image (screen, window, data->pixmap, &filename, &geom);
674   screen_to_texture_async_cb (screen, window, data->pixmap, filename, &geom,
675                               data);
676
677   return (*image_width_return != 0);
678 }
679
680
681 /* Like the above, but the image is loaded in a background process,
682    and a callback is run when the loading is complete.
683    When the callback is called, the image data will have been loaded
684    into texture number `texid' (via glBindTexture.)
685
686    If an error occurred, width/height will be 0.
687  */
688 void
689 screen_to_texture_async (Screen *screen, Window window,
690                          int desired_width, int desired_height,
691                          Bool mipmap_p,
692                          GLuint texid,
693                          void (*callback) (const char *filename,
694                                            XRectangle *geometry,
695                                            int image_width,
696                                            int image_height,
697                                            int texture_width,
698                                            int texture_height,
699                                            void *closure),
700                          void *closure)
701 {
702   Display *dpy = DisplayOfScreen (screen);
703   XWindowAttributes xgwa;
704   img_closure *data = (img_closure *) calloc (1, sizeof(*data));
705
706   if (debug_p)
707     data->load_time = double_time();
708
709   data->texid     = texid;
710   data->mipmap_p  = mipmap_p;
711   data->callback  = callback;
712   data->closure   = closure;
713
714   XGetWindowAttributes (dpy, window, &xgwa);
715   data->pix_width  = xgwa.width;
716   data->pix_height = xgwa.height;
717   data->pix_depth  = xgwa.depth;
718
719   if (desired_width  && desired_width  < xgwa.width)
720     data->pix_width  = desired_width;
721   if (desired_height && desired_height < xgwa.height)
722     data->pix_height = desired_height;
723
724   data->pixmap = XCreatePixmap (dpy, window, data->pix_width, data->pix_height,
725                                 data->pix_depth);
726   fork_load_random_image (screen, window, data->pixmap,
727                           screen_to_texture_async_cb, data);
728 }
729
730
731 /* Once we have an XImage, this loads it into GL.
732    This is used in both synchronous and asynchronous mode.
733  */
734 static void
735 screen_to_texture_async_cb (Screen *screen, Window window, Drawable drawable,
736                             const char *name, XRectangle *geometry,
737                             void *closure)
738 {
739   Display *dpy = DisplayOfScreen (screen);
740   Bool ok;
741   XImage *ximage;
742   GLint type, format;
743   int iw=0, ih=0, tw=0, th=0;
744   double cvt_time=0, tex_time=0, done_time=0;
745   img_closure *data = (img_closure *) closure;
746   /* copy closure data to stack and free the original before running cb */
747   img_closure dd = *data;
748   memset (data, 0, sizeof (*data));
749   free (data);
750   data = 0;
751
752   if (geometry->width <= 0 || geometry->height <= 0)
753     {
754       /* This can happen if an old version of xscreensaver-getimage
755          is installed. */
756       geometry->x = 0;
757       geometry->y = 0;
758       geometry->width  = dd.pix_width;
759       geometry->height = dd.pix_height;
760     }
761
762   if (geometry->width <= 0 || geometry->height <= 0)
763     abort();
764
765   if (debug_p)
766     cvt_time = double_time();
767
768 # ifdef REFORMAT_IMAGE_DATA
769   ximage = pixmap_to_gl_ximage (screen, window, dd.pixmap);
770   format = GL_RGBA;
771   type = GL_UNSIGNED_BYTE;
772
773 #else /* ! REFORMAT_IMAGE_DATA */
774   {
775     Visual *visual = DefaultVisualOfScreen (screen);
776     GLint swap;
777
778     ximage = XCreateImage (dpy, visual, dd.pix_depth, ZPixmap, 0, 0,
779                            dd.pix_width, dd.pix_height, 32, 0);
780
781     /* Note: height+2 in "to" to be to work around an array bounds overrun
782        in gluBuild2DMipmaps / gluScaleImage. */
783     ximage->data = (char *) calloc (ximage->height+2, ximage->bytes_per_line);
784
785     if (!ximage->data ||
786         !XGetSubImage (dpy, dd.pixmap, 0, 0, ximage->width, ximage->height,
787                        ~0L, ximage->format, ximage, 0, 0))
788       {
789         XDestroyImage (ximage);
790         ximage = 0;
791       }
792
793     gl_settings_for_ximage (ximage, &type, &format, &swap);
794     glPixelStorei (GL_UNPACK_SWAP_BYTES, !swap);
795   }
796 #endif /* REFORMAT_IMAGE_DATA */
797
798   XFreePixmap (dpy, dd.pixmap);
799   dd.pixmap = 0;
800
801   if (debug_p)
802     tex_time = double_time();
803
804   if (! ximage)
805     ok = False;
806   else
807     {
808       iw = ximage->width;
809       ih = ximage->height;
810       if (dd.texid != -1)
811         glBindTexture (GL_TEXTURE_2D, dd.texid);
812
813       glPixelStorei (GL_UNPACK_ALIGNMENT, ximage->bitmap_pad / 8);
814       ok = ximage_to_texture (ximage, type, format, &tw, &th, geometry,
815                               dd.mipmap_p);
816       if (ok)
817         {
818           iw = ximage->width;   /* in case the image was shrunk */
819           ih = ximage->height;
820         }
821     }
822
823   if (ximage) XDestroyImage (ximage);
824
825   if (! ok)
826     iw = ih = tw = th = 0;
827
828   if (debug_p)
829     done_time = double_time();
830
831   if (debug_p)
832     fprintf (stderr,
833              /* prints: A + B + C = D
834                 A = file I/O time (happens in background)
835                 B = time to pull bits from server (this process)
836                 C = time to convert bits to GL textures (this process)
837                 D = total elapsed time from "want image" to "see image"
838
839                 B+C is responsible for any frame-rate glitches.
840               */
841              "%s: loading elapsed: %.2f + %.2f + %.2f = %.2f sec\n",
842              progname,
843              cvt_time  - dd.load_time,
844              tex_time  - cvt_time,
845              done_time - tex_time,
846              done_time - dd.load_time);
847
848   if (dd.callback)
849     /* asynchronous mode */
850     dd.callback (name, geometry, iw, ih, tw, th, dd.closure);
851   else
852     {
853       /* synchronous mode */
854       if (dd.filename_return)       *dd.filename_return       = (char *) name;
855       if (dd.geometry_return)       *dd.geometry_return       = *geometry;
856       if (dd.image_width_return)    *dd.image_width_return    = iw;
857       if (dd.image_height_return)   *dd.image_height_return   = ih;
858       if (dd.texture_width_return)  *dd.texture_width_return  = tw;
859       if (dd.texture_height_return) *dd.texture_height_return = th;
860     }
861 }