Initial Commit
[packages] / xemacs-packages / oo-browser / tree-x / dbl.c
1 /* ----------------------------------------------------------------------------
2  *     Double buffering code
3  * ----------------------------------------------------------------------------
4  */
5
6
7 #include <stdlib.h>
8 #include "dbl.h"
9
10 struct {
11     unsigned short red;
12     unsigned short green;
13     unsigned short blue;
14 } color[] = {
15     { 65280, 65280, 65280 }, /* white  */
16     {     0,     0, 65280 }, /* blue   */
17     {     0, 65280,     0 }, /* green  */
18     { 65280,     0,     0 }, /* red    */
19     { 42240, 10752, 10752 }, /* brown  */
20     { 65280, 32512,     0 }, /* orange */
21     { 32512, 32512, 32512 }, /* gray   */
22     {     0,     0,     0 }  /* black  */
23 };
24
25 /* ------------------------------------------------------------------------- */
26
27 DoubleBuffer *
28 DBLcreate_double_buffer (Display *display,
29                          Window   window,
30                          int      backing_store,
31                          XColor  *colors,
32                          int      num_colors)
33 {
34    int i, j, k, l, offset, mask, size;
35    int max_planes;
36
37    char         *string;
38    Surface     *surface;
39    DoubleBuffer     *db;
40    XGCValues       xgcv;
41    unsigned long   xgcvmask;
42
43 /* allocate the double buffer structure,  and then open the display */
44
45    if ((db = (DoubleBuffer *)calloc(1, sizeof(DoubleBuffer))) == 0) {
46       printf("DBLopen_double_buffer : memory allocation error\n");
47       return NULL;
48    }
49
50 /* note the display */
51
52    db->display  = display;
53
54 /* first some information about our display */
55
56    db->screen   = DefaultScreenOfDisplay(db->display);
57    db->window   = window;
58
59 /* now get some information on color resources */
60
61    db->visual   = DefaultVisualOfScreen(db->screen);
62    db->depth    = DefaultDepthOfScreen(db->screen);
63    db->colormap = DefaultColormapOfScreen(db->screen);
64
65 /* set up colors */
66
67    for (i = 0 ; i < num_colors; i++) {
68       color[i].red   = colors[i].red;
69       color[i].green = colors[i].green;
70       color[i].blue  = colors[i].blue;
71    }
72
73 /* see if the user wanted to limit the number of planes used
74    then see how many are available,  make it a multiple of 2 */
75
76    if ((string = getenv("DBL_MAX_PLANES")) == NULL)
77       max_planes = DBL_MAX_PLANES;
78    else {
79       max_planes = atoi(string);
80    }
81
82    if ((db->num_planes = PlanesOfScreen(db->screen)) > max_planes) {
83       db->num_planes = max_planes;
84    }
85
86    db->num_planes = (db->num_planes >> 1) << 1;
87
88
89 /* otherwise allocate contiguous planes to do double buffering */
90
91    switch (db->visual->class) {
92    case PseudoColor:
93    case DirectColor:
94    case GrayScale:
95      /* XAllocColorCells cannot be used with TrueColor */
96      while (db->num_planes >= DBL_MIN_PLANES) {
97        if (XAllocColorCells (db->display, db->colormap, 1, db->planes,
98                              db->num_planes, db->pixels, 1)) {
99          break;
100        }
101
102        db->num_planes -= 2;
103      }
104      break;
105    case TrueColor:
106      /* Disable double buffering */
107      db->num_surfaces = 0;
108      
109      db->num_colors = DBL_MAX_COLORS;
110      db->colors[0]  = WhitePixelOfScreen(db->screen);
111      for (i = 1; i < num_colors; i++) {
112        /* I have no idea how to fail gracefully.  If we're running in 24bit */
113        /* this call can't fail, right? */
114        if (XAllocColor(db->display, db->colormap, &colors[i])) {
115          db->colors[i] = colors[i].pixel;
116        }
117      }
118
119      goto cont;
120    default: /* Mono color? */
121      db->num_planes = 2;
122      break;
123    }
124
125 /* if we have at least minimum planes, then we can do double
126    buffering and we want to setup our surfaces and colormaps */
127
128    if (db->num_planes < DBL_MIN_PLANES)
129       db->num_surfaces = 0;
130    else {
131       db->num_colors   = 1 << (db->num_planes >> 1);
132       db->num_surfaces = DBL_MAX_SURFACES;
133
134    /* if the number of colors is less than DBL_MAX_COLORS,
135       then we want to make sure black is the  last  color */
136
137       for (i = db->num_colors - 1; i < DBL_MAX_COLORS; i++) {
138          color[i].red   = color[DBL_MAX_COLORS - 1].red;
139          color[i].green = color[DBL_MAX_COLORS - 1].green;
140          color[i].blue  = color[DBL_MAX_COLORS - 1].blue;
141       }
142
143    /* we have a set of contiguous planes.  compute a mask for
144       the planes,  and figure out the offset in the  hardware */
145
146       for (i = 0; i < db->num_planes; i++) {
147          db->mask |= db->planes[i];
148       }
149
150       mask   = db->mask;
151       offset = 0;
152
153       while ((mask & 1) == 0) {
154          mask = mask >> 1;
155          offset = offset + 1;
156       }
157
158       mask = (1 << (db->num_planes >> 1)) - 1;
159
160    /* now create the surfaces that will contain plane mask and
161       colormap information that we use to do double  buffering */
162
163       for (i = 0; i < db->num_surfaces; i++) {
164          size = sizeof(Surface) + sizeof(XColor) * (1 << db->num_planes);
165
166          if ((surface = (Surface *)malloc(size)) != NULL)
167             db->surface[i] = surface;
168          else {
169             printf("DBLcreate_double_buffer : memory allocation error\n");
170             DBLdelete_double_buffer(db);
171             return NULL;
172          }
173
174          surface->offset     = offset + i * (db->num_planes >> 1);
175          surface->mask       = mask << surface->offset;
176          surface->num_colors = 1 << db->num_planes;
177
178       /* compute our pixel values by taking every permutation
179          of the pixel and planes returned by XAllocColorCells */
180
181          for (j = 0; j < (surface->num_colors); j++) {
182             surface->color[j].pixel = db->pixels[0];
183          }
184
185          for (j = 0; j < db->num_planes; j++) {
186             for (k = (1 << j); k < (surface->num_colors); k += (2 << j)) {
187                for (l = k; l < (k + (1 << j)); l++) {
188                   surface->color[l].pixel |= db->planes[j];
189                }
190             }
191          }
192
193        /* now populate those pixels with the proper colors so
194           that we can do animation by banging in a new colormap */
195
196          for (j = 0; j < surface->num_colors; j++) {
197             k = (j & surface->mask) >> surface->offset;
198
199             surface->color[j].red   = color[k].red;
200             surface->color[j].green = color[k].green;
201             surface->color[j].blue  = color[k].blue;
202
203             surface->color[j].flags = DoRed | DoGreen | DoBlue;
204          }
205       }
206
207       db->current_surface = 0;
208    }
209
210
211 /* now figure out what pixel values we will use to draw with
212    and store them in the double buffer structure */
213
214    if (db->num_surfaces == 0) {
215       db->num_colors = DBL_MAX_COLORS;
216       db->colors[0]  = WhitePixelOfScreen(db->screen);
217
218       for (i = 1; i < db->num_colors; i++) {
219          db->colors[i] = BlackPixelOfScreen(db->screen);
220       }
221    }
222    else {
223       for (i = 0; i < db->num_colors; i++) {
224          j = (i << (db->num_planes >> 1)) + i;
225          db->colors[i] = db->surface[0]->color[j].pixel;
226       }
227    }
228
229 /* fill out the remaining colors with the last color */
230
231    for (; i < DBL_MAX_COLORS; i++) {
232       db->colors[i] = db->colors[db->num_colors - 1];
233    }
234
235 cont:
236    /* Switched to micro size double buffer to eliminate major bug of window
237       display inverting during startup.  This is just a workaround until
238       a real solution is found.  -- Bob Weiner, 02/02/97. 
239       db->width   = WidthOfScreen(db->screen);
240       db->height  = HeightOfScreen(db->screen);
241     */
242
243    db->width   = 1;
244    db->height  = 1;
245
246 /* if there are no surfaces then we are doing animation with
247    a frame buffer,  so create a pixmap as our frame buffer   */
248
249    if (db->num_surfaces > 0)
250       db->drawable = db->window;
251    else {
252       db->frame = XCreatePixmap(db->display, db->window,
253                                 db->width, db->height, db->depth);
254       db->drawable = db->frame;
255    }
256
257 /* if they have requested backing store,  then create an extra
258    pixmap which we can use as backing store to handle exposures */
259
260    if (backing_store) {
261       db->backing = XCreatePixmap(db->display, db->window,
262                                   db->width, db->height, db->depth);
263    }
264
265 /*  use the 0 pixel from one of the surfaces for the background */
266
267    xgcv.background = DBLinq_background(db);
268    xgcv.line_style = LineSolid;
269    xgcv.line_width = 0;
270    xgcv.cap_style  = CapButt;
271    xgcv.join_style = JoinRound;
272    xgcvmask = GCBackground | GCLineStyle | GCLineWidth | GCCapStyle |
273               GCJoinStyle;
274
275    db->gc = XCreateGC(db->display, db->drawable, xgcvmask, &xgcv);
276
277 /* do an initial frame to setup the colormap,  and return */
278
279    DBLbegin_frame(db);
280    DBLend_frame(db, 1);
281    return db;
282 }
283
284 /* ------------------------------------------------------------------------- */
285
286 void
287 DBLdelete_double_buffer (DoubleBuffer *db)
288 {
289   int i;
290
291   /* remove and and all surfaces that are out there */
292
293   for (i = 0; i < DBL_MAX_SURFACES; i++) {
294     if (db->surface[i] != 0) {
295       free(db->surface[i]);
296     }
297   }
298
299   /* now clean up the various resources used for this double buffer */
300
301   if (db->frame != 0) {
302     XFreePixmap(db->display, db->frame);
303   }
304
305   if (db->backing != 0) {
306     XFreePixmap(db->display, db->backing);
307   }
308
309   /* if we created our own private colormap,  then free the colormap */
310
311   if (db->colormap != DefaultColormapOfScreen(db->screen)) {
312     XFreeColormap(db->display, db->colormap);
313   }
314
315   free (db);
316 }
317
318 /* ------------------------------------------------------------------------- */
319
320 unsigned long
321 DBLinq_background(DoubleBuffer *db)
322 {
323    if (db->num_surfaces > 0)
324       return db->surface[0]->color[0].pixel;
325    else
326       return WhitePixelOfScreen(db->screen);
327 }
328
329 /* ------------------------------------------------------------------------- */
330
331 void
332 DBLbegin_frame(DoubleBuffer *db)
333 {
334   /* there will be at most two surfaces optimized with "&"*/
335
336   if (db->num_surfaces > 0) {
337     db->current_surface = (db->current_surface + 1) & 1;
338
339     /* clear the back surface of the window which may actually be a pixmap */
340     
341     XSetPlaneMask (db->display, db->gc, db->surface[db->current_surface]->mask);
342   }
343
344   /* clear out the back surface or frame buffer as appropriate */
345
346   XSetFunction(db->display, db->gc, GXclear);
347   XFillRectangle(db->display, db->drawable, db->gc,
348                  0, 0, db->width, db->height);
349
350   /* set writing mode back to copy */
351   XSetFunction (db->display, db->gc, GXcopy);
352
353   XSync(db->display, False);
354 }
355
356
357 /* ------------------------------------------------------------------------- */
358
359 void
360 DBLend_frame(DoubleBuffer *db, short init)
361 {
362   Surface  *surface;
363
364   /* if there are no drawing surfaces, then we are doing animation
365      with a frame buffer, copy the frame buffers to their viewports */
366
367   if (db->num_surfaces == 0) {
368      if (! init)
369         XCopyArea (db->display, db->frame, db->window,
370                    db->gc, 0,0, db->width, db->height, 0,0);
371   } else {
372
373     /* otherwise,  we can flip the surface by banging in the new colormap */
374
375     XSync(db->display, False);
376     surface = db->surface[db->current_surface];
377     XStoreColors (db->display, db->colormap,
378                   surface->color, surface->num_colors);
379   }
380
381   if (db->backing != 0) {
382     XCopyArea (db->display, db->window, db->backing,
383                db->gc, 0,0, db->width, db->height, 0,0);
384   }
385
386   /* make sure this all goes off to the server,  right away */
387
388   XSync(db->display, False);
389 }