Mercurial > hg > xemacs-beta
comparison pkg-src/tree-x/dbl.c @ 163:0132846995bd r20-3b8
Import from CVS: tag r20-3b8
author | cvs |
---|---|
date | Mon, 13 Aug 2007 09:43:35 +0200 |
parents | |
children | 85ec50267440 |
comparison
equal
deleted
inserted
replaced
162:4de2936b4e77 | 163:0132846995bd |
---|---|
1 /* ---------------------------------------------------------------------------- | |
2 * Double buffering code | |
3 * ---------------------------------------------------------------------------- | |
4 */ | |
5 | |
6 | |
7 #include "dbl.h" | |
8 | |
9 struct { | |
10 unsigned short red; | |
11 unsigned short green; | |
12 unsigned short blue; | |
13 } color[] = { | |
14 { 65280, 65280, 65280 }, /* white */ | |
15 { 0, 0, 65280 }, /* blue */ | |
16 { 0, 65280, 0 }, /* green */ | |
17 { 65280, 0, 0 }, /* red */ | |
18 { 42240, 10752, 10752 }, /* brown */ | |
19 { 65280, 32512, 0 }, /* orange */ | |
20 { 32512, 32512, 32512 }, /* gray */ | |
21 { 0, 0, 0 } /* black */ | |
22 }; | |
23 | |
24 /* ------------------------------------------------------------------------- */ | |
25 | |
26 DoubleBuffer * | |
27 DBLcreate_double_buffer (display, window, backing_store, colors, num_colors) | |
28 Display *display; | |
29 Window window; | |
30 int backing_store; | |
31 XColor *colors; | |
32 int num_colors; | |
33 { | |
34 int i, j, k, l, m, 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 while (db->num_planes >= DBL_MIN_PLANES) { | |
92 if (XAllocColorCells (db->display, db->colormap, 1, db->planes, | |
93 db->num_planes, db->pixels, 1)) { | |
94 break; | |
95 } | |
96 | |
97 db->num_planes -= 2; | |
98 } | |
99 | |
100 /* if we have at least minimum planes, then we can do double | |
101 buffering and we want to setup our surfaces and colormaps */ | |
102 | |
103 if (db->num_planes < DBL_MIN_PLANES) | |
104 db->num_surfaces = 0; | |
105 else { | |
106 db->num_colors = 1 << (db->num_planes >> 1); | |
107 db->num_surfaces = DBL_MAX_SURFACES; | |
108 | |
109 /* if the number of colors is less than DBL_MAX_COLORS, | |
110 then we want to make sure black is the last color */ | |
111 | |
112 for (i = db->num_colors - 1; i < DBL_MAX_COLORS; i++) { | |
113 color[i].red = color[DBL_MAX_COLORS - 1].red; | |
114 color[i].green = color[DBL_MAX_COLORS - 1].green; | |
115 color[i].blue = color[DBL_MAX_COLORS - 1].blue; | |
116 } | |
117 | |
118 /* we have a set of contiguous planes. compute a mask for | |
119 the planes, and figure out the offset in the hardware */ | |
120 | |
121 for (i = 0; i < db->num_planes; i++) { | |
122 db->mask |= db->planes[i]; | |
123 } | |
124 | |
125 mask = db->mask; | |
126 offset = 0; | |
127 | |
128 while ((mask & 1) == 0) { | |
129 mask = mask >> 1; | |
130 offset = offset + 1; | |
131 } | |
132 | |
133 mask = (1 << (db->num_planes >> 1)) - 1; | |
134 | |
135 /* now create the surfaces that will contain plane mask and | |
136 colormap information that we use to do double buffering */ | |
137 | |
138 for (i = 0; i < db->num_surfaces; i++) { | |
139 size = sizeof(Surface) + sizeof(XColor) * (1 << db->num_planes); | |
140 | |
141 if ((surface = (Surface *)malloc(size)) != NULL) | |
142 db->surface[i] = surface; | |
143 else { | |
144 printf("DBLcreate_double_buffer : memory allocation error\n"); | |
145 DBLdelete_double_buffer(db); | |
146 return(NULL); | |
147 } | |
148 | |
149 surface->offset = offset + i * (db->num_planes >> 1); | |
150 surface->mask = mask << surface->offset; | |
151 surface->num_colors = 1 << db->num_planes; | |
152 | |
153 /* compute our pixel values by taking every permutation | |
154 of the pixel and planes returned by XAllocColorCells */ | |
155 | |
156 for (j = 0; j < (surface->num_colors); j++) { | |
157 surface->color[j].pixel = db->pixels[0]; | |
158 } | |
159 | |
160 for (j = 0; j < db->num_planes; j++) { | |
161 for (k = (1 << j); k < (surface->num_colors); k += (2 << j)) { | |
162 for (l = k; l < (k + (1 << j)); l++) { | |
163 surface->color[l].pixel |= db->planes[j]; | |
164 } | |
165 } | |
166 } | |
167 | |
168 /* now populate those pixels with the proper colors so | |
169 that we can do animation by banging in a new colormap */ | |
170 | |
171 for (j = 0; j < surface->num_colors; j++) { | |
172 k = (j & surface->mask) >> surface->offset; | |
173 | |
174 surface->color[j].red = color[k].red; | |
175 surface->color[j].green = color[k].green; | |
176 surface->color[j].blue = color[k].blue; | |
177 | |
178 surface->color[j].flags = DoRed | DoGreen | DoBlue; | |
179 } | |
180 } | |
181 | |
182 db->current_surface = 0; | |
183 } | |
184 | |
185 | |
186 /* now figure out what pixel values we will use to draw with | |
187 and store them in the double buffer structure */ | |
188 | |
189 if (db->num_surfaces == 0) { | |
190 db->num_colors = DBL_MAX_COLORS; | |
191 db->colors[0] = WhitePixelOfScreen(db->screen); | |
192 | |
193 for (i = 1; i < db->num_colors; i++) { | |
194 db->colors[i] = BlackPixelOfScreen(db->screen); | |
195 } | |
196 } | |
197 else { | |
198 for (i = 0; i < db->num_colors; i++) { | |
199 j = (i << (db->num_planes >> 1)) + i; | |
200 db->colors[i] = db->surface[0]->color[j].pixel; | |
201 } | |
202 } | |
203 | |
204 /* fill out the remaining colors with the last color */ | |
205 | |
206 for (; i < DBL_MAX_COLORS; i++) { | |
207 db->colors[i] = db->colors[db->num_colors - 1]; | |
208 } | |
209 | |
210 db->width = WidthOfScreen(db->screen); | |
211 db->height = HeightOfScreen(db->screen); | |
212 | |
213 /* if there are no surfaces then we are doing animation with | |
214 a frame buffer, so create a pixmap as our frame buffer */ | |
215 | |
216 if (db->num_surfaces > 0) | |
217 db->drawable = db->window; | |
218 else { | |
219 db->frame = XCreatePixmap(db->display, db->window, | |
220 db->width, db->height, db->depth); | |
221 db->drawable = db->frame; | |
222 } | |
223 | |
224 /* if they have requested backing store, then create an extra | |
225 pixmap which we can use as backing store to handle exposures */ | |
226 | |
227 if (backing_store) { | |
228 db->backing = XCreatePixmap(db->display, db->window, | |
229 db->width, db->height, db->depth); | |
230 } | |
231 | |
232 /* use the 0 pixel from one of the surfaces for the background */ | |
233 | |
234 xgcv.background = DBLinq_background(db); | |
235 xgcv.line_style = LineSolid; | |
236 xgcv.line_width = 0; | |
237 xgcv.cap_style = CapButt; | |
238 xgcv.join_style = JoinRound; | |
239 xgcvmask = GCBackground | GCLineStyle | GCLineWidth | GCCapStyle | | |
240 GCJoinStyle; | |
241 | |
242 db->gc = XCreateGC(db->display, db->drawable, xgcvmask, &xgcv); | |
243 | |
244 /* do an initial frame to setup the colormap, and return */ | |
245 | |
246 DBLbegin_frame(db); | |
247 DBLend_frame(db, 1); | |
248 return (db); | |
249 } | |
250 | |
251 /* ------------------------------------------------------------------------- */ | |
252 | |
253 void | |
254 DBLdelete_double_buffer (db) | |
255 DoubleBuffer *db; | |
256 { | |
257 int i; | |
258 | |
259 /* remove and and all surfaces that are out there */ | |
260 | |
261 for (i = 0; i < DBL_MAX_SURFACES; i++) { | |
262 if (db->surface[i] != 0) { | |
263 free(db->surface[i]); | |
264 } | |
265 } | |
266 | |
267 /* now clean up the various resources used for this double buffer */ | |
268 | |
269 if (db->frame != 0) { | |
270 XFreePixmap(db->display, db->frame); | |
271 } | |
272 | |
273 if (db->backing != 0) { | |
274 XFreePixmap(db->display, db->backing); | |
275 } | |
276 | |
277 /* if we created our own private colormap, then free the colormap */ | |
278 | |
279 if (db->colormap != DefaultColormapOfScreen(db->screen)) { | |
280 XFreeColormap(db->display, db->colormap); | |
281 } | |
282 | |
283 free (db); | |
284 } | |
285 | |
286 /* ------------------------------------------------------------------------- */ | |
287 | |
288 unsigned long | |
289 DBLinq_background(db) | |
290 DoubleBuffer *db; | |
291 { | |
292 if (db->num_surfaces > 0) | |
293 return(db->surface[0]->color[0].pixel); | |
294 else | |
295 return(WhitePixelOfScreen(db->screen)); | |
296 } | |
297 | |
298 /* ------------------------------------------------------------------------- */ | |
299 | |
300 DBLbegin_frame(db) | |
301 DoubleBuffer *db; | |
302 { | |
303 Surface *surface; | |
304 | |
305 /* there will be at most two surfaces optimize with "&"*/ | |
306 | |
307 if (db->num_surfaces > 0) { | |
308 db->current_surface = (db->current_surface + 1) & 1; | |
309 surface = db->surface[db->current_surface]; | |
310 } | |
311 | |
312 /* clear the back surface of the window which may actually be a pixmap */ | |
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 */ | |
318 | |
319 XSetFunction(db->display, db->gc, GXclear); | |
320 XFillRectangle(db->display, db->drawable, db->gc, | |
321 0, 0, db->width, db->height); | |
322 | |
323 /* set writing mode back to copy */ | |
324 XSetFunction (db->display, db->gc, GXcopy); | |
325 | |
326 XSync(db->display, False); | |
327 } | |
328 | |
329 | |
330 /* ------------------------------------------------------------------------- */ | |
331 | |
332 | |
333 DBLend_frame(db, init) | |
334 DoubleBuffer *db; | |
335 short init; | |
336 { | |
337 Surface *surface; | |
338 | |
339 /* if there are no drawing surfaces, then we are doing animation | |
340 with a frame buffer, copy the frame buffers to their viewports */ | |
341 | |
342 if (db->num_surfaces == 0) { | |
343 if (! init) | |
344 XCopyArea (db->display, db->frame, db->window, | |
345 db->gc, 0,0, db->width, db->height, 0,0); | |
346 } else { | |
347 | |
348 /* otherwise, we can flip the surface by banging in the new colormap */ | |
349 | |
350 XSync(db->display, False); | |
351 surface = db->surface[db->current_surface]; | |
352 XStoreColors (db->display, db->colormap, | |
353 surface->color, surface->num_colors); | |
354 } | |
355 | |
356 if (db->backing != 0) { | |
357 XCopyArea (db->display, db->window, db->backing, | |
358 db->gc, 0,0, db->width, db->height, 0,0); | |
359 } | |
360 | |
361 /* make sure this all goes off to the server, right away */ | |
362 | |
363 XSync(db->display, False); | |
364 } | |
365 | |
366 | |
367 | |
368 | |
369 | |
370 | |
371 | |
372 | |
373 | |
374 | |
375 | |
376 | |
377 |