summaryrefslogtreecommitdiff
path: root/src/mesa/pipe/xlib/xm_surface.c
blob: 23945630959c40b2511e497d16f2f260988bc08d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/*
 * Mesa 3-D graphics library
 * Version:  7.1
 *
 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


/**
 * \file xm_surface.c
 * Code to allow the softpipe code to write to X windows/buffers.
 * This is a bit of a hack for now.  We've basically got two different
 * abstractions for color buffers: gl_renderbuffer and pipe_surface.
 * They'll need to get merged someday...
 * For now, they're separate things that point to each other.
 */


#include "GL/xmesa.h"
#include "glxheader.h"
#include "xmesaP.h"
#include "main/context.h"
#include "main/imports.h"
#include "main/macros.h"

#include "pipe/p_context.h"
#include "pipe/p_defines.h"
#include "pipe/p_winsys.h"
#include "pipe/softpipe/sp_context.h"
#include "pipe/softpipe/sp_clear.h"
#include "pipe/softpipe/sp_tile_cache.h"
#include "pipe/softpipe/sp_surface.h"
#include "state_tracker/st_context.h"


/*
 * Dithering kernels and lookup tables.
 */

const int xmesa_kernel8[DITH_DY * DITH_DX] = {
    0 * MAXC,  8 * MAXC,  2 * MAXC, 10 * MAXC,
   12 * MAXC,  4 * MAXC, 14 * MAXC,  6 * MAXC,
    3 * MAXC, 11 * MAXC,  1 * MAXC,  9 * MAXC,
   15 * MAXC,  7 * MAXC, 13 * MAXC,  5 * MAXC,
};

const int xmesa_kernel1[16] = {
   0*47,  9*47,  4*47, 12*47,     /* 47 = (255*3)/16 */
   6*47,  2*47, 14*47,  8*47,
  10*47,  1*47,  5*47, 11*47,
   7*47, 13*47,  3*47, 15*47
};



#define CLIP_TILE \
   do { \
      if (x >= ps->width) \
         return; \
      if (y >= ps->height) \
         return; \
      if (x + w > ps->width) \
         w = ps->width - x; \
      if (y + h > ps->height) \
         h = ps->height -y; \
   } while(0)



/*
 * The following functions are used to trap XGetImage() calls which
 * generate BadMatch errors if the drawable isn't mapped.
 */

#ifndef XFree86Server
static int caught_xgetimage_error = 0;
static int (*old_xerror_handler)( XMesaDisplay *dpy, XErrorEvent *ev );
static unsigned long xgetimage_serial;

/*
 * This is the error handler which will be called if XGetImage fails.
 */
static int xgetimage_error_handler( XMesaDisplay *dpy, XErrorEvent *ev )
{
   if (ev->serial==xgetimage_serial && ev->error_code==BadMatch) {
      /* caught the expected error */
      caught_xgetimage_error = 0;
   }
   else {
      /* call the original X error handler, if any.  otherwise ignore */
      if (old_xerror_handler) {
         (*old_xerror_handler)( dpy, ev );
      }
   }
   return 0;
}


/*
 * Call this right before XGetImage to setup error trap.
 */
static void catch_xgetimage_errors( XMesaDisplay *dpy )
{
   xgetimage_serial = NextRequest( dpy );
   old_xerror_handler = XSetErrorHandler( xgetimage_error_handler );
   caught_xgetimage_error = 0;
}


/*
 * Call this right after XGetImage to check if an error occured.
 */
static int check_xgetimage_errors( void )
{
   /* restore old handler */
   (void) XSetErrorHandler( old_xerror_handler );
   /* return 0=no error, 1=error caught */
   return caught_xgetimage_error;
}
#endif


/**
 * Wrapper for XGetImage() that catches BadMatch errors that can occur
 * when the window is unmapped or the x/y/w/h extend beyond the window
 * bounds.
 * If build into xserver, wrap the internal GetImage method.
 */
static XMesaImage *
xget_image(XMesaDisplay *dpy, Drawable d, int x, int y, uint w, uint h)
{
#ifdef XFree86Server
   uint bpp = 4; /* XXX fix this */
   XMesaImage *ximage = (XMesaImage *) malloc(sizeof(XMesaImage));
   if (ximage) {
      ximage->data = malloc(width * height * bpp);
   }
   (*dpy->GetImage)(d, x, y, w, h, ZPixmap, ~0L, (pointer)ximage->data);
   ximage->width = w;
   ximage->height = h;
   ximage->bytes_per_row = w * bpp;
   return ximage;
#else
   int error;
   XMesaImage *ximage;
   catch_xgetimage_errors(dpy);
   ximage = XGetImage(dpy, d, x, y, w, h, AllPlanes, ZPixmap);
   error = check_xgetimage_errors();
   return ximage;
#endif
}



/**
 * Return raw pixels from pixmap or XImage.
 */
void
xmesa_get_tile(struct pipe_context *pipe, struct pipe_surface *ps,
               uint x, uint y, uint w, uint h, void *p, int dst_stride)
{
   const uint w0 = w;
   struct xmesa_surface *xms = xmesa_surface(ps);
   XMesaImage *ximage = NULL;
   ubyte *dst = (ubyte *) p;
   uint i;

   if (!xms->drawable && !xms->ximage) {
      /* not an X surface */
      softpipe_get_tile(pipe, ps, x, y, w, h, p, dst_stride);
      return;
   }

   CLIP_TILE;

   if (!xms->ximage) {
      /* XImage = pixmap data */
      assert(xms->drawable);
      ximage = xget_image(xms->display, xms->drawable, x, y, w, h);
      if (!ximage)
         return;
      x = y = 0;
   }
   else {
      ximage = xms->ximage;
   }

   /* this could be optimized/simplified */
   switch (ps->format) {
   case PIPE_FORMAT_U_A8_R8_G8_B8:
      if (!dst_stride) {
         dst_stride = w0 * 4;
      }
      for (i = 0; i < h; i++) {
         memcpy(dst, ximage->data + y * ximage->bytes_per_line + x * 4, 4 * w);
         dst += dst_stride;
      }
      break;
   case PIPE_FORMAT_U_R5_G6_B5:
      if (!dst_stride) {
         dst_stride = w0 * 2;
      }
      for (i = 0; i < h; i++) {
         memcpy(dst, ximage->data + y * ximage->bytes_per_line + x * 2, 4 * 2);
         dst += dst_stride;
      }
      break;
   default:
      assert(0);
   }

   if (!xms->ximage) {
      XMesaDestroyImage(ximage);
   }
}


/**
 * Put raw pixels into pixmap or XImage.
 */
void
xmesa_put_tile(struct pipe_context *pipe, struct pipe_surface *ps,
               uint x, uint y, uint w, uint h, const void *p, int src_stride)
{
   const uint w0 = w;
   struct xmesa_surface *xms = xmesa_surface(ps);
   const ubyte *src = (const ubyte *) p;
   XMesaImage *ximage;

   if (!xms->drawable && !xms->ximage) {
      /* not an X surface */
      softpipe_put_tile(pipe, ps, x, y, w, h, p, src_stride);
      return;
   }

   CLIP_TILE;

   if (xms->ximage) {
      /* put to ximage */
      ximage = xms->ximage;
      char *dst;
      int i;

      /* this could be optimized/simplified */
      switch (ps->format) {
      case PIPE_FORMAT_U_A8_R8_G8_B8:
         if (!src_stride) {
            src_stride = w0 * 4;
         }
         dst = ximage->data + y * ximage->bytes_per_line + x * 4;
         for (i = 0; i < h; i++) {
            memcpy(dst, src, w * 4);
            dst += ximage->bytes_per_line;
            src += src_stride;
         }
         break;
      case PIPE_FORMAT_U_R5_G6_B5:
         if (!src_stride) {
            src_stride = w0 * 2;
         }
         dst = ximage->data + y * ximage->bytes_per_line + x * 2;
         for (i = 0; i < h; i++) {
            memcpy(dst, src, w * 2);
            dst += ximage->bytes_per_line;
            src += src_stride;
         }
         break;
      default:
         assert(0);
      }
   }
   else {
      /* put to pixmap/window */
      /* Create temp XImage for data */
#ifdef XFree86Server
      ximage = XMesaCreateImage(GET_VISUAL_DEPTH(v), w, h, p);
#else
      XVisualInfo *visinfo = xms->xrb->Parent->xm_visual->visinfo;
      ximage = XCreateImage(xms->display,
                            visinfo->visual,
                            visinfo->depth,
                            ZPixmap, 0,   /* format, offset */
                            (char *) p,   /* data */
                            w, h,         /* width, height */
                            32,           /* bitmap_pad */
                            0);           /* bytes_per_line */
#endif

      /* send XImage data to pixmap */
      XPutImage(xms->display, xms->drawable, xms->gc,
                ximage, 0, 0, x, y, w, h);
      /* clean-up */
      ximage->data = NULL; /* prevents freeing user data at 'p' */
      XMesaDestroyImage(ximage);
   }
}


void
xmesa_get_tile_rgba(struct pipe_context *pipe, struct pipe_surface *ps,
                    uint x, uint y, uint w, uint h, float *pixels)
{
   const uint w0 = w;
   struct xmesa_surface *xms = xmesa_surface(ps);
   XMesaImage *ximage = NULL;
   float *pRow = pixels;
   uint i, j;

   if (!xms->drawable && !xms->ximage) {
      /* not an X surface */
      softpipe_get_tile_rgba(pipe, ps, x, y, w, h, pixels);
      return;
   }

   CLIP_TILE;

   if (!xms->ximage) {
      /* XImage = pixmap data */
      assert(xms->drawable);
      ximage = xget_image(xms->display, xms->drawable, x, y, w, h);
      if (!ximage)
         return;
      x = y = 0;
   }
   else {
      ximage = xms->ximage;
   }
   
   switch (ps->format) {
   case PIPE_FORMAT_U_A8_R8_G8_B8:
      {
         const uint *src
            = (uint *) (ximage->data + y * ximage->bytes_per_line + x * 4);
         for (i = 0; i < h; i++) {
            float *p = pRow;
            for (j = 0; j < w; j++) {
               uint pix = src[j];
               ubyte r = ((pix >> 16) & 0xff);
               ubyte g = ((pix >> 8)  & 0xff);
               ubyte b = ( pix        & 0xff);
               ubyte a = ((pix >> 24) & 0xff);
               p[0] = UBYTE_TO_FLOAT(r);
               p[1] = UBYTE_TO_FLOAT(g);
               p[2] = UBYTE_TO_FLOAT(b);
               p[3] = UBYTE_TO_FLOAT(a);
               p += 4;
            }
            src += ximage->width;
            pRow += 4 * w0;
         }
      }
      break;
   case PIPE_FORMAT_U_R5_G6_B5:
      {
         ushort *src
            = (ushort *) (ximage->data + y * ximage->bytes_per_line + x * 2);
         for (i = 0; i < h; i++) {
            float *p = pRow;
            for (j = 0; j < w; j++) {
               uint pix = src[j];
               ubyte r = (pix >> 8) | ((pix >> 13) & 0x7);
               ubyte g = (pix >> 3) | ((pix >>  9) & 0x3);
               ubyte b = ((pix & 0x1f) << 3) | ((pix >> 2) & 0x3);
               p[0] = UBYTE_TO_FLOAT(r);
               p[1] = UBYTE_TO_FLOAT(g);
               p[2] = UBYTE_TO_FLOAT(b);
               p[3] = 1.0;
               p += 4;
            }
            src += ximage->width;
            pRow += 4 * w0;
         }
      }
      break;
   default:
      assert(0);
   }

   if (!xms->ximage) {
      XMesaDestroyImage(ximage);
   }
}


void
xmesa_put_tile_rgba(struct pipe_context *pipe, struct pipe_surface *ps,
                    uint x, uint y, uint w, uint h, const float *pixels)
{
   const uint x0 = x, y0 = y, w0 = w;
   struct xmesa_surface *xms = xmesa_surface(ps);
   XMesaImage *ximage;
   uint i, j;

   if (!xms->drawable && !xms->ximage) {
      /* not an X surface */
      softpipe_put_tile_rgba(pipe, ps, x, y, w, h, pixels);
      return;
   }

   CLIP_TILE;

   if (!xms->ximage) {
      /* create temp XImage */
      char *data = (char *) malloc(w * h * 4);
#ifdef XFree86Server
      ximage = XMesaCreateImage(GET_VISUAL_DEPTH(v), w, h, data);
#else
      XVisualInfo *visinfo = xms->xrb->Parent->xm_visual->visinfo;
      ximage = XCreateImage(xms->display,
                            visinfo->visual,
                            visinfo->depth,
                            ZPixmap, 0,   /* format, offset */
                            data,         /* data */
                            w, h,         /* width, height */
                            32,           /* bitmap_pad */
                            0);           /* bytes_per_line */
#endif
      x = y = 0;
   }
   else {
      ximage = xms->ximage;
   }

   /* convert floats to ximage's format */
   switch (ps->format) {
   case PIPE_FORMAT_U_A8_R8_G8_B8:
      {
         uint *dst
            = (uint *) (ximage->data + y * ximage->bytes_per_line + x * 4);
         const float *pRow = pixels;
         for (i = 0; i < h; i++) {
            const float *p = pRow;
            for (j = 0; j < w; j++) {
               ubyte r, g, b, a;
               UNCLAMPED_FLOAT_TO_UBYTE(r, p[0]);
               UNCLAMPED_FLOAT_TO_UBYTE(g, p[1]);
               UNCLAMPED_FLOAT_TO_UBYTE(b, p[2]);
               UNCLAMPED_FLOAT_TO_UBYTE(a, p[3]);
               dst[j] = PACK_8A8R8G8B(r, g, b, a);
               p += 4;
            }
            dst += ximage->width;
            pRow += 4 * w0;
         }
      }
      break;
   case PIPE_FORMAT_U_R5_G6_B5:
      {
         ushort *dst =
            (ushort *) (ximage->data + y * ximage->bytes_per_line + x * 2);
         const float *pRow = pixels;
         for (i = 0; i < h; i++) {
            const float *p = pRow;
            for (j = 0; j < w; j++) {
               ubyte r, g, b;
               UNCLAMPED_FLOAT_TO_UBYTE(r, p[0]);
               UNCLAMPED_FLOAT_TO_UBYTE(g, p[1]);
               UNCLAMPED_FLOAT_TO_UBYTE(b, p[2]);
               dst[j] = PACK_5R6G5B(r, g, b);
               p += 4;
            }
            dst += ximage->width;
            pRow += 4 * w0;
         }
      }
      break;
      
   default:
      assert(0);
   }

   if (!xms->ximage) {
      /* send XImage data to pixmap */
      XPutImage(xms->display, xms->drawable, xms->gc,
                ximage, 0, 0, x0, y0, w, h);
      /* clean-up */
      free(ximage->data);
      ximage->data = NULL;
      XMesaDestroyImage(ximage);
   }
}


static void
clear_pixmap_surface(struct pipe_context *pipe, struct pipe_surface *ps,
                     uint value)
{
   struct xmesa_surface *xms = xmesa_surface(ps);
   assert(xms);
   assert(xms->display);
   assert(xms->drawable);
   assert(xms->gc);
   XMesaSetForeground( xms->display, xms->gc, value );
   XMesaFillRectangle( xms->display, xms->drawable, xms->gc,
                       0, 0, ps->width, ps->height);
}

static void
clear_nbit_ximage_surface(struct pipe_context *pipe, struct pipe_surface *ps,
                          uint value)
{
   struct xmesa_surface *xms = xmesa_surface(ps);
   int width = xms->surface.width;
   int height = xms->surface.height;
   int i, j;
   for (j = 0; j < height; j++) {
      for (i = 0; i < width; i++) {
         XMesaPutPixel(xms->ximage, i, j, value);
      }
   }
}

static void
clear_8bit_ximage_surface(struct pipe_context *pipe, struct pipe_surface *ps,
                          uint value)
{
   struct xmesa_surface *xms = xmesa_surface(ps);
   memset(xms->ximage->data,
          value,
          xms->ximage->bytes_per_line * xms->ximage->height);
}

static void
clear_16bit_ximage_surface(struct pipe_context *pipe, struct pipe_surface *ps,
                           uint value)
{
   struct xmesa_surface *xms = xmesa_surface(ps);
   const int n = xms->ximage->width * xms->ximage->height;
   ushort *dst = (ushort *) xms->ximage->data;
   int i;
   for (i = 0; i < n; i++) {
      dst[i] = value;
   }
}


/* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
static void
clear_24bit_ximage_surface(struct pipe_context *pipe, struct pipe_surface *ps,
                           uint value)
{
   struct xmesa_surface *xms = xmesa_surface(ps);
   const ubyte r = (value      ) & 0xff;
   const ubyte g = (value >>  8) & 0xff;
   const ubyte b = (value >> 16) & 0xff;

   if (r == g && g == b) {
      /* same value for all three components (gray) */
      memset(xms->ximage->data, r,
             xms->ximage->bytes_per_line * xms->ximage->height);
   }
   else {
      /* non-gray clear color */
      const int n = xms->ximage->width * xms->ximage->height;
      int i;
      bgr_t *ptr3 = (bgr_t *) xms->ximage->data;
      for (i = 0; i < n; i++) {
         ptr3->r = r;
         ptr3->g = g;
         ptr3->b = b;
         ptr3++;
      }
   }
}

static void
clear_32bit_ximage_surface(struct pipe_context *pipe, struct pipe_surface *ps,
                           uint value)
{
   struct xmesa_surface *xms = xmesa_surface(ps);

   if (value == 0) {
      /* common case */
      memset(xms->ximage->data, value,
             xms->ximage->bytes_per_line * xms->ximage->height);
   }
   else {
      const int n = xms->ximage->width * xms->ximage->height;
      uint *dst = (uint *) xms->ximage->data;
      int i;
      for (i = 0; i < n; i++)
         dst[i] = value;
   }
}




/**
 * Called to create a pipe_surface for each X renderbuffer.
 * Note: this is being used instead of pipe->surface_alloc() since we
 * have special/unique quad read/write functions for X.
 */
struct pipe_surface *
xmesa_new_color_surface(struct pipe_winsys *winsys, GLuint pipeFormat)
{
   struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);

   assert(pipeFormat);

   xms->surface.format = pipeFormat;
   xms->surface.refcount = 1;
   xms->surface.winsys = winsys;

   /* Note, the region we allocate doesn't actually have any storage
    * since we're drawing into an XImage or Pixmap.
    * The region's size will get set in the xmesa_alloc_front/back_storage()
    * functions.
    */
   xms->surface.region = winsys->region_alloc(winsys, 1, 0x0);

   return &xms->surface;
}


/**
 * Called via pipe->surface_alloc() to create new surfaces (textures,
 * renderbuffers, etc.
 */
struct pipe_surface *
xmesa_surface_alloc(struct pipe_context *pipe, GLuint pipeFormat)
{
   struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);

   assert(pipe);
   assert(pipeFormat);

   xms->surface.format = pipeFormat;
   xms->surface.refcount = 1;
   xms->surface.winsys = pipe->winsys;

   return &xms->surface;
}


/**
 * Called via pipe->clear() to clear entire surface to a certain value.
 * If the surface is not an X pixmap or XImage, pass the call to
 * softpipe_clear().
 */
void
xmesa_clear(struct pipe_context *pipe, struct pipe_surface *ps, uint value)
{
   struct xmesa_surface *xms = xmesa_surface(ps);

   /* XXX actually, we should just discard any cached tiles from this
    * surface since we don't want to accidentally re-use them after clearing.
    */
   pipe->flush(pipe, 0);

   {
      struct softpipe_context *sp = softpipe_context(pipe);
      if (ps == sp_tile_cache_get_surface(sp->cbuf_cache[0])) {
         float clear[4];
         clear[0] = 0.2; /* XXX hack */
         clear[1] = 0.2;
         clear[2] = 0.2;
         clear[3] = 0.2;
         sp_tile_cache_clear(sp->cbuf_cache[0], clear);
      }
   }

#if 1
   (void) clear_8bit_ximage_surface;
   (void) clear_24bit_ximage_surface;
#endif

   if (xms->ximage) {
      /* back color buffer */
      switch (xms->surface.format) {
      case PIPE_FORMAT_U_R5_G6_B5:
         clear_16bit_ximage_surface(pipe, ps, value);
         break;
      case PIPE_FORMAT_U_A8_R8_G8_B8:
         clear_32bit_ximage_surface(pipe, ps, value);
         break;
      default:
         clear_nbit_ximage_surface(pipe, ps, value);
         break;
      }
   }
   else if (xms->drawable) {
      /* front color buffer */
      clear_pixmap_surface(pipe, ps, value);
   }
   else {
      /* other kind of buffer */
      softpipe_clear(pipe, ps, value);
   }
}


/** XXX unfinished sketch... */
struct pipe_surface *
xmesa_create_front_surface(XMesaVisual vis, Window win)
{
   struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);
   if (!xms) {
      return NULL;
   }

   xms->display = vis->display;
   xms->drawable = win;

   xms->surface.format = PIPE_FORMAT_U_A8_R8_G8_B8;
   xms->surface.refcount = 1;
#if 0
   xms->surface.region = pipe->winsys->region_alloc(pipe->winsys,
                                                    1, 0, 0, 0x0);
#endif
   return &xms->surface;
}