1 /* ----------------------------------------------------------------------------
2 * Double buffering code
3 * ----------------------------------------------------------------------------
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 */
25 /* ------------------------------------------------------------------------- */
28 DBLcreate_double_buffer (Display *display,
34 int i, j, k, l, offset, mask, size;
41 unsigned long xgcvmask;
43 /* allocate the double buffer structure, and then open the display */
45 if ((db = (DoubleBuffer *)calloc(1, sizeof(DoubleBuffer))) == 0) {
46 printf("DBLopen_double_buffer : memory allocation error\n");
50 /* note the display */
52 db->display = display;
54 /* first some information about our display */
56 db->screen = DefaultScreenOfDisplay(db->display);
59 /* now get some information on color resources */
61 db->visual = DefaultVisualOfScreen(db->screen);
62 db->depth = DefaultDepthOfScreen(db->screen);
63 db->colormap = DefaultColormapOfScreen(db->screen);
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;
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 */
76 if ((string = getenv("DBL_MAX_PLANES")) == NULL)
77 max_planes = DBL_MAX_PLANES;
79 max_planes = atoi(string);
82 if ((db->num_planes = PlanesOfScreen(db->screen)) > max_planes) {
83 db->num_planes = max_planes;
86 db->num_planes = (db->num_planes >> 1) << 1;
89 /* otherwise allocate contiguous planes to do double buffering */
91 switch (db->visual->class) {
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)) {
106 /* Disable double buffering */
107 db->num_surfaces = 0;
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;
120 default: /* Mono color? */
125 /* if we have at least minimum planes, then we can do double
126 buffering and we want to setup our surfaces and colormaps */
128 if (db->num_planes < DBL_MIN_PLANES)
129 db->num_surfaces = 0;
131 db->num_colors = 1 << (db->num_planes >> 1);
132 db->num_surfaces = DBL_MAX_SURFACES;
134 /* if the number of colors is less than DBL_MAX_COLORS,
135 then we want to make sure black is the last color */
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;
143 /* we have a set of contiguous planes. compute a mask for
144 the planes, and figure out the offset in the hardware */
146 for (i = 0; i < db->num_planes; i++) {
147 db->mask |= db->planes[i];
153 while ((mask & 1) == 0) {
158 mask = (1 << (db->num_planes >> 1)) - 1;
160 /* now create the surfaces that will contain plane mask and
161 colormap information that we use to do double buffering */
163 for (i = 0; i < db->num_surfaces; i++) {
164 size = sizeof(Surface) + sizeof(XColor) * (1 << db->num_planes);
166 if ((surface = (Surface *)malloc(size)) != NULL)
167 db->surface[i] = surface;
169 printf("DBLcreate_double_buffer : memory allocation error\n");
170 DBLdelete_double_buffer(db);
174 surface->offset = offset + i * (db->num_planes >> 1);
175 surface->mask = mask << surface->offset;
176 surface->num_colors = 1 << db->num_planes;
178 /* compute our pixel values by taking every permutation
179 of the pixel and planes returned by XAllocColorCells */
181 for (j = 0; j < (surface->num_colors); j++) {
182 surface->color[j].pixel = db->pixels[0];
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];
193 /* now populate those pixels with the proper colors so
194 that we can do animation by banging in a new colormap */
196 for (j = 0; j < surface->num_colors; j++) {
197 k = (j & surface->mask) >> surface->offset;
199 surface->color[j].red = color[k].red;
200 surface->color[j].green = color[k].green;
201 surface->color[j].blue = color[k].blue;
203 surface->color[j].flags = DoRed | DoGreen | DoBlue;
207 db->current_surface = 0;
211 /* now figure out what pixel values we will use to draw with
212 and store them in the double buffer structure */
214 if (db->num_surfaces == 0) {
215 db->num_colors = DBL_MAX_COLORS;
216 db->colors[0] = WhitePixelOfScreen(db->screen);
218 for (i = 1; i < db->num_colors; i++) {
219 db->colors[i] = BlackPixelOfScreen(db->screen);
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;
229 /* fill out the remaining colors with the last color */
231 for (; i < DBL_MAX_COLORS; i++) {
232 db->colors[i] = db->colors[db->num_colors - 1];
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);
246 /* if there are no surfaces then we are doing animation with
247 a frame buffer, so create a pixmap as our frame buffer */
249 if (db->num_surfaces > 0)
250 db->drawable = db->window;
252 db->frame = XCreatePixmap(db->display, db->window,
253 db->width, db->height, db->depth);
254 db->drawable = db->frame;
257 /* if they have requested backing store, then create an extra
258 pixmap which we can use as backing store to handle exposures */
261 db->backing = XCreatePixmap(db->display, db->window,
262 db->width, db->height, db->depth);
265 /* use the 0 pixel from one of the surfaces for the background */
267 xgcv.background = DBLinq_background(db);
268 xgcv.line_style = LineSolid;
270 xgcv.cap_style = CapButt;
271 xgcv.join_style = JoinRound;
272 xgcvmask = GCBackground | GCLineStyle | GCLineWidth | GCCapStyle |
275 db->gc = XCreateGC(db->display, db->drawable, xgcvmask, &xgcv);
277 /* do an initial frame to setup the colormap, and return */
284 /* ------------------------------------------------------------------------- */
287 DBLdelete_double_buffer (DoubleBuffer *db)
291 /* remove and and all surfaces that are out there */
293 for (i = 0; i < DBL_MAX_SURFACES; i++) {
294 if (db->surface[i] != 0) {
295 free(db->surface[i]);
299 /* now clean up the various resources used for this double buffer */
301 if (db->frame != 0) {
302 XFreePixmap(db->display, db->frame);
305 if (db->backing != 0) {
306 XFreePixmap(db->display, db->backing);
309 /* if we created our own private colormap, then free the colormap */
311 if (db->colormap != DefaultColormapOfScreen(db->screen)) {
312 XFreeColormap(db->display, db->colormap);
318 /* ------------------------------------------------------------------------- */
321 DBLinq_background(DoubleBuffer *db)
323 if (db->num_surfaces > 0)
324 return db->surface[0]->color[0].pixel;
326 return WhitePixelOfScreen(db->screen);
329 /* ------------------------------------------------------------------------- */
332 DBLbegin_frame(DoubleBuffer *db)
334 /* there will be at most two surfaces optimized with "&"*/
336 if (db->num_surfaces > 0) {
337 db->current_surface = (db->current_surface + 1) & 1;
339 /* clear the back surface of the window which may actually be a pixmap */
341 XSetPlaneMask (db->display, db->gc, db->surface[db->current_surface]->mask);
344 /* clear out the back surface or frame buffer as appropriate */
346 XSetFunction(db->display, db->gc, GXclear);
347 XFillRectangle(db->display, db->drawable, db->gc,
348 0, 0, db->width, db->height);
350 /* set writing mode back to copy */
351 XSetFunction (db->display, db->gc, GXcopy);
353 XSync(db->display, False);
357 /* ------------------------------------------------------------------------- */
360 DBLend_frame(DoubleBuffer *db, short init)
364 /* if there are no drawing surfaces, then we are doing animation
365 with a frame buffer, copy the frame buffers to their viewports */
367 if (db->num_surfaces == 0) {
369 XCopyArea (db->display, db->frame, db->window,
370 db->gc, 0,0, db->width, db->height, 0,0);
373 /* otherwise, we can flip the surface by banging in the new colormap */
375 XSync(db->display, False);
376 surface = db->surface[db->current_surface];
377 XStoreColors (db->display, db->colormap,
378 surface->color, surface->num_colors);
381 if (db->backing != 0) {
382 XCopyArea (db->display, db->window, db->backing,
383 db->gc, 0,0, db->width, db->height, 0,0);
386 /* make sure this all goes off to the server, right away */
388 XSync(db->display, False);