comparison pkg-src/tree-x/dbl.c @ 167:85ec50267440 r20-3b10

Import from CVS: tag r20-3b10
author cvs
date Mon, 13 Aug 2007 09:45:46 +0200
parents 0132846995bd
children 15872534500d
comparison
equal deleted inserted replaced
166:7a77eb660975 167:85ec50267440
2 * Double buffering code 2 * Double buffering code
3 * ---------------------------------------------------------------------------- 3 * ----------------------------------------------------------------------------
4 */ 4 */
5 5
6 6
7 #include <stdlib.h>
7 #include "dbl.h" 8 #include "dbl.h"
8 9
9 struct { 10 struct {
10 unsigned short red; 11 unsigned short red;
11 unsigned short green; 12 unsigned short green;
12 unsigned short blue; 13 unsigned short blue;
13 } color[] = { 14 } color[] = {
14 { 65280, 65280, 65280 }, /* white */ 15 { 65280, 65280, 65280 }, /* white */
15 { 0, 0, 65280 }, /* blue */ 16 { 0, 0, 65280 }, /* blue */
16 { 0, 65280, 0 }, /* green */ 17 { 0, 65280, 0 }, /* green */
17 { 65280, 0, 0 }, /* red */ 18 { 65280, 0, 0 }, /* red */
18 { 42240, 10752, 10752 }, /* brown */ 19 { 42240, 10752, 10752 }, /* brown */
19 { 65280, 32512, 0 }, /* orange */ 20 { 65280, 32512, 0 }, /* orange */
20 { 32512, 32512, 32512 }, /* gray */ 21 { 32512, 32512, 32512 }, /* gray */
21 { 0, 0, 0 } /* black */ 22 { 0, 0, 0 } /* black */
22 }; 23 };
23 24
24 /* ------------------------------------------------------------------------- */ 25 /* ------------------------------------------------------------------------- */
25 26
26 DoubleBuffer * 27 DoubleBuffer *
27 DBLcreate_double_buffer (display, window, backing_store, colors, num_colors) 28 DBLcreate_double_buffer (Display *display,
28 Display *display; 29 Window window,
29 Window window; 30 int backing_store,
30 int backing_store; 31 XColor *colors,
31 XColor *colors; 32 int num_colors)
32 int num_colors; 33 {
33 {
34 int i, j, k, l, m, offset, mask, size; 34 int i, j, k, l, m, offset, mask, size;
35 int max_planes; 35 int max_planes;
36 36
37 char *string; 37 char *string;
38 Surface *surface; 38 Surface *surface;
43 /* allocate the double buffer structure, and then open the display */ 43 /* allocate the double buffer structure, and then open the display */
44 44
45 if ((db = (DoubleBuffer *)calloc(1, sizeof(DoubleBuffer))) == 0) { 45 if ((db = (DoubleBuffer *)calloc(1, sizeof(DoubleBuffer))) == 0) {
46 printf("DBLopen_double_buffer : memory allocation error\n"); 46 printf("DBLopen_double_buffer : memory allocation error\n");
47 return (NULL); 47 return (NULL);
48 } 48 }
49 49
50 /* note the display */ 50 /* note the display */
51 51
52 db->display = display; 52 db->display = display;
53 53
67 for (i = 0 ; i < num_colors; i++) { 67 for (i = 0 ; i < num_colors; i++) {
68 color[i].red = colors[i].red; 68 color[i].red = colors[i].red;
69 color[i].green = colors[i].green; 69 color[i].green = colors[i].green;
70 color[i].blue = colors[i].blue; 70 color[i].blue = colors[i].blue;
71 } 71 }
72 72
73 /* see if the user wanted to limit the number of planes used 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 */ 74 then see how many are available, make it a multiple of 2 */
75 75
76 if ((string = getenv("DBL_MAX_PLANES")) == NULL) 76 if ((string = getenv("DBL_MAX_PLANES")) == NULL)
77 max_planes = DBL_MAX_PLANES; 77 max_planes = DBL_MAX_PLANES;
128 while ((mask & 1) == 0) { 128 while ((mask & 1) == 0) {
129 mask = mask >> 1; 129 mask = mask >> 1;
130 offset = offset + 1; 130 offset = offset + 1;
131 } 131 }
132 132
133 mask = (1 << (db->num_planes >> 1)) - 1; 133 mask = (1 << (db->num_planes >> 1)) - 1;
134 134
135 /* now create the surfaces that will contain plane mask and 135 /* now create the surfaces that will contain plane mask and
136 colormap information that we use to do double buffering */ 136 colormap information that we use to do double buffering */
137 137
138 for (i = 0; i < db->num_surfaces; i++) { 138 for (i = 0; i < db->num_surfaces; i++) {
147 } 147 }
148 148
149 surface->offset = offset + i * (db->num_planes >> 1); 149 surface->offset = offset + i * (db->num_planes >> 1);
150 surface->mask = mask << surface->offset; 150 surface->mask = mask << surface->offset;
151 surface->num_colors = 1 << db->num_planes; 151 surface->num_colors = 1 << db->num_planes;
152 152
153 /* compute our pixel values by taking every permutation 153 /* compute our pixel values by taking every permutation
154 of the pixel and planes returned by XAllocColorCells */ 154 of the pixel and planes returned by XAllocColorCells */
155 155
156 for (j = 0; j < (surface->num_colors); j++) { 156 for (j = 0; j < (surface->num_colors); j++) {
157 surface->color[j].pixel = db->pixels[0]; 157 surface->color[j].pixel = db->pixels[0];
158 } 158 }
159 159
160 for (j = 0; j < db->num_planes; j++) { 160 for (j = 0; j < db->num_planes; j++) {
161 for (k = (1 << j); k < (surface->num_colors); k += (2 << j)) { 161 for (k = (1 << j); k < (surface->num_colors); k += (2 << j)) {
162 for (l = k; l < (k + (1 << j)); l++) { 162 for (l = k; l < (k + (1 << j)); l++) {
163 surface->color[l].pixel |= db->planes[j]; 163 surface->color[l].pixel |= db->planes[j];
164 } 164 }
165 } 165 }
166 } 166 }
167 167
168 /* now populate those pixels with the proper colors so 168 /* now populate those pixels with the proper colors so
169 that we can do animation by banging in a new colormap */ 169 that we can do animation by banging in a new colormap */
170 170
171 for (j = 0; j < surface->num_colors; j++) { 171 for (j = 0; j < surface->num_colors; j++) {
172 k = (j & surface->mask) >> surface->offset; 172 k = (j & surface->mask) >> surface->offset;
173 173
174 surface->color[j].red = color[k].red; 174 surface->color[j].red = color[k].red;
175 surface->color[j].green = color[k].green; 175 surface->color[j].green = color[k].green;
176 surface->color[j].blue = color[k].blue; 176 surface->color[j].blue = color[k].blue;
177 177
178 surface->color[j].flags = DoRed | DoGreen | DoBlue; 178 surface->color[j].flags = DoRed | DoGreen | DoBlue;
179 } 179 }
210 db->width = WidthOfScreen(db->screen); 210 db->width = WidthOfScreen(db->screen);
211 db->height = HeightOfScreen(db->screen); 211 db->height = HeightOfScreen(db->screen);
212 212
213 /* if there are no surfaces then we are doing animation with 213 /* if there are no surfaces then we are doing animation with
214 a frame buffer, so create a pixmap as our frame buffer */ 214 a frame buffer, so create a pixmap as our frame buffer */
215 215
216 if (db->num_surfaces > 0) 216 if (db->num_surfaces > 0)
217 db->drawable = db->window; 217 db->drawable = db->window;
218 else { 218 else {
219 db->frame = XCreatePixmap(db->display, db->window, 219 db->frame = XCreatePixmap(db->display, db->window,
220 db->width, db->height, db->depth); 220 db->width, db->height, db->depth);
221 db->drawable = db->frame; 221 db->drawable = db->frame;
222 } 222 }
223 223
224 /* if they have requested backing store, then create an extra 224 /* if they have requested backing store, then create an extra
225 pixmap which we can use as backing store to handle exposures */ 225 pixmap which we can use as backing store to handle exposures */
226 226
227 if (backing_store) { 227 if (backing_store) {
228 db->backing = XCreatePixmap(db->display, db->window, 228 db->backing = XCreatePixmap(db->display, db->window,
229 db->width, db->height, db->depth); 229 db->width, db->height, db->depth);
230 } 230 }
231 231
232 /* use the 0 pixel from one of the surfaces for the background */ 232 /* use the 0 pixel from one of the surfaces for the background */
233 233
234 xgcv.background = DBLinq_background(db); 234 xgcv.background = DBLinq_background(db);
235 xgcv.line_style = LineSolid; 235 xgcv.line_style = LineSolid;
236 xgcv.line_width = 0; 236 xgcv.line_width = 0;
237 xgcv.cap_style = CapButt; 237 xgcv.cap_style = CapButt;
238 xgcv.join_style = JoinRound; 238 xgcv.join_style = JoinRound;
239 xgcvmask = GCBackground | GCLineStyle | GCLineWidth | GCCapStyle | 239 xgcvmask = GCBackground | GCLineStyle | GCLineWidth | GCCapStyle |
240 GCJoinStyle; 240 GCJoinStyle;
241 241
242 db->gc = XCreateGC(db->display, db->drawable, xgcvmask, &xgcv); 242 db->gc = XCreateGC(db->display, db->drawable, xgcvmask, &xgcv);
243 243
244 /* do an initial frame to setup the colormap, and return */ 244 /* do an initial frame to setup the colormap, and return */
245 245
246 DBLbegin_frame(db); 246 DBLbegin_frame(db);
249 } 249 }
250 250
251 /* ------------------------------------------------------------------------- */ 251 /* ------------------------------------------------------------------------- */
252 252
253 void 253 void
254 DBLdelete_double_buffer (db) 254 DBLdelete_double_buffer (DoubleBuffer *db)
255 DoubleBuffer *db;
256 { 255 {
257 int i; 256 int i;
258 257
259 /* remove and and all surfaces that are out there */ 258 /* remove and and all surfaces that are out there */
260 259
261 for (i = 0; i < DBL_MAX_SURFACES; i++) { 260 for (i = 0; i < DBL_MAX_SURFACES; i++) {
262 if (db->surface[i] != 0) { 261 if (db->surface[i] != 0) {
263 free(db->surface[i]); 262 free(db->surface[i]);
264 } 263 }
265 } 264 }
266 265
267 /* now clean up the various resources used for this double buffer */ 266 /* now clean up the various resources used for this double buffer */
268 267
269 if (db->frame != 0) { 268 if (db->frame != 0) {
270 XFreePixmap(db->display, db->frame); 269 XFreePixmap(db->display, db->frame);
271 } 270 }
272 271
273 if (db->backing != 0) { 272 if (db->backing != 0) {
274 XFreePixmap(db->display, db->backing); 273 XFreePixmap(db->display, db->backing);
275 } 274 }
276 275
277 /* if we created our own private colormap, then free the colormap */ 276 /* if we created our own private colormap, then free the colormap */
278 277
279 if (db->colormap != DefaultColormapOfScreen(db->screen)) { 278 if (db->colormap != DefaultColormapOfScreen(db->screen)) {
280 XFreeColormap(db->display, db->colormap); 279 XFreeColormap(db->display, db->colormap);
281 } 280 }
282 281
283 free (db); 282 free (db);
284 } 283 }
285 284
286 /* ------------------------------------------------------------------------- */ 285 /* ------------------------------------------------------------------------- */
287 286
288 unsigned long 287 unsigned long
289 DBLinq_background(db) 288 DBLinq_background(DoubleBuffer *db)
290 DoubleBuffer *db;
291 { 289 {
292 if (db->num_surfaces > 0) 290 if (db->num_surfaces > 0)
293 return(db->surface[0]->color[0].pixel); 291 return(db->surface[0]->color[0].pixel);
294 else 292 else
295 return(WhitePixelOfScreen(db->screen)); 293 return(WhitePixelOfScreen(db->screen));
296 } 294 }
297 295
298 /* ------------------------------------------------------------------------- */ 296 /* ------------------------------------------------------------------------- */
299 297
300 DBLbegin_frame(db) 298 void
301 DoubleBuffer *db; 299 DBLbegin_frame(DoubleBuffer *db)
302 { 300 {
303 Surface *surface; 301 /* there will be at most two surfaces optimized with "&"*/
304 302
305 /* there will be at most two surfaces optimize with "&"*/ 303 if (db->num_surfaces > 0) {
306 304 db->current_surface = (db->current_surface + 1) & 1;
307 if (db->num_surfaces > 0) { 305
308 db->current_surface = (db->current_surface + 1) & 1; 306 /* clear the back surface of the window which may actually be a pixmap */
309 surface = db->surface[db->current_surface]; 307
310 } 308 XSetPlaneMask (db->display, db->gc, db->surface[db->current_surface]->mask);
311 309 }
312 /* clear the back surface of the window which may actually be a pixmap */ 310
313
314 if (db->num_surfaces > 0)
315 XSetPlaneMask (db->display, db->gc, surface->mask);
316
317 /* clear out the back surface or frame buffer as appropriate */ 311 /* clear out the back surface or frame buffer as appropriate */
318 312
319 XSetFunction(db->display, db->gc, GXclear); 313 XSetFunction(db->display, db->gc, GXclear);
320 XFillRectangle(db->display, db->drawable, db->gc, 314 XFillRectangle(db->display, db->drawable, db->gc,
321 0, 0, db->width, db->height); 315 0, 0, db->width, db->height);
322 316
323 /* set writing mode back to copy */ 317 /* set writing mode back to copy */
324 XSetFunction (db->display, db->gc, GXcopy); 318 XSetFunction (db->display, db->gc, GXcopy);
325 319
326 XSync(db->display, False); 320 XSync(db->display, False);
327 } 321 }
328 322
329 323
330 /* ------------------------------------------------------------------------- */ 324 /* ------------------------------------------------------------------------- */
331 325
332 326 void
333 DBLend_frame(db, init) 327 DBLend_frame(DoubleBuffer *db, short init)
334 DoubleBuffer *db;
335 short init;
336 { 328 {
337 Surface *surface; 329 Surface *surface;
338 330
339 /* if there are no drawing surfaces, then we are doing animation 331 /* if there are no drawing surfaces, then we are doing animation
340 with a frame buffer, copy the frame buffers to their viewports */ 332 with a frame buffer, copy the frame buffers to their viewports */
341 333
342 if (db->num_surfaces == 0) { 334 if (db->num_surfaces == 0) {
343 if (! init) 335 if (! init)
344 XCopyArea (db->display, db->frame, db->window, 336 XCopyArea (db->display, db->frame, db->window,
345 db->gc, 0,0, db->width, db->height, 0,0); 337 db->gc, 0,0, db->width, db->height, 0,0);
346 } else { 338 } else {
347 339
348 /* otherwise, we can flip the surface by banging in the new colormap */ 340 /* otherwise, we can flip the surface by banging in the new colormap */
349 341
350 XSync(db->display, False); 342 XSync(db->display, False);
351 surface = db->surface[db->current_surface]; 343 surface = db->surface[db->current_surface];
352 XStoreColors (db->display, db->colormap, 344 XStoreColors (db->display, db->colormap,
355 347
356 if (db->backing != 0) { 348 if (db->backing != 0) {
357 XCopyArea (db->display, db->window, db->backing, 349 XCopyArea (db->display, db->window, db->backing,
358 db->gc, 0,0, db->width, db->height, 0,0); 350 db->gc, 0,0, db->width, db->height, 0,0);
359 } 351 }
360 352
361 /* make sure this all goes off to the server, right away */ 353 /* make sure this all goes off to the server, right away */
362 354
363 XSync(db->display, False); 355 XSync(db->display, False);
364 } 356 }
365
366
367
368
369
370
371
372
373
374
375
376
377