File: FXGLVisual.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (825 lines) | stat: -rw-r--r-- 27,130 bytes parent folder | download | duplicates (2)
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/********************************************************************************
*                                                                               *
*                            V i s u a l   C l a s s                            *
*                                                                               *
*********************************************************************************
* Copyright (C) 1999,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library is distributed in the hope that it will be useful,               *
* but WITHOUT ANY WARRANTY; without even the implied warranty of                *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXElement.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXException.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXAccelTable.h"
#include "FXFont.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXApp.h"
#include "FXGLVisual.h"


/*
  Notes:

  - FXGLVisual builds a visual/pixelformat suitable for GL drawing.

  - Selection of visual/pixelformat is based on a best match to a
    given set of hints, according to some heuristics:

    1) Prefer color depth close to asked ones; really like to
       get a bit MORE color, rather than LESS, however.

    2) If we wanted Z-buffer, it is STRONGLY preferred; If there's
       a choice, we prefer more color depth over more Z-depth; if
       we already have more colors than requested, we prefer to meet
       requested Z depth.

    3) If we wanted double buffer, we strongly prefer it over color and
       Z depth, but HAVING a Z-buffer is still more important.

    4) If we wanted alpha buffer, it is preferred, but Z-buffering
       and double buffering are considered more important.
       If there's a choice, we prefer to receive a few MORE bits of
       alpha buffer than we asked for, rather than LESS.

    5) If we wanted stereo, we prefer it, but almost everything except
       the color-, alpha-, and Z-depths are more important.

  - Some further tuning may be desired, but I think this should satisfy
    most cases....

  - Note that as long as OpenGL is in any way supported, you should ALWAYS
    be able to get at least some visual/pixelformat you can draw on.

  - As far as hardware acceleration goes, H/W acceleration should be
    enabled, possibly at the expense of color-, alpha-, and Z-depth;
    but NEVER at the expense of presence or absence of a requested feature.
    We only drop FEATURES which are requested if there is neither hardware
    nor software support.

    For example, we may trade in some Z-depth, but not the entire Z-buffer,
    to get a hardware accelerated visual/pixelformat.
*/


using namespace FX;

/*******************************************************************************/

namespace FX {

// Object implementation
FXIMPLEMENT(FXGLVisual,FXVisual,nullptr,0)


// Deserialization
FXGLVisual::FXGLVisual(){
  redSize=0;
  greenSize=0;
  blueSize=0;
  alphaSize=0;
  depthSize=0;
  stencilSize=0;
  multiSamples=0;
  accumRedSize=0;
  accumGreenSize=0;
  accumBlueSize=0;
  accumAlphaSize=0;
  actualRedSize=0;
  actualGreenSize=0;
  actualBlueSize=0;
  actualAlphaSize=0;
  actualDepthSize=0;
  actualStencilSize=0;
  actualMultiSamples=0;
  actualAccumRedSize=0;
  actualAccumGreenSize=0;
  actualAccumBlueSize=0;
  actualAccumAlphaSize=0;
  doubleBuffer=false;
  stereoBuffer=false;
  accelerated=false;
  copying=false;
  }


// Construct
FXGLVisual::FXGLVisual(FXApp* a,FXuint flgs):FXVisual(a,flgs,0){
  FXTRACE((100,"FXGLVisual::FXGLVisual %p\n",this));
  redSize=8;
  greenSize=8;
  blueSize=8;
  alphaSize=0;
  depthSize=24;
  stencilSize=0;
  multiSamples=0;
  accumRedSize=0;
  accumGreenSize=0;
  accumBlueSize=0;
  accumAlphaSize=0;
  actualRedSize=0;
  actualGreenSize=0;
  actualBlueSize=0;
  actualAlphaSize=0;
  actualDepthSize=0;
  actualStencilSize=0;
  actualMultiSamples=0;
  actualAccumRedSize=0;
  actualAccumGreenSize=0;
  actualAccumBlueSize=0;
  actualAccumAlphaSize=0;
  doubleBuffer=false;
  stereoBuffer=false;
  accelerated=false;
  copying=false;
  }


// Test if OpenGL is possible
FXbool FXGLVisual::hasOpenGL(FXApp* application){
  if(application->isInitialized()){
#ifdef HAVE_GL_H
#ifdef WIN32            // WIN32
    return true;
#else                   // UNIX
    return glXQueryExtension((Display*)application->getDisplay(),nullptr,nullptr);
#endif
#endif
    }
  return false;
  }


/*******************************************************************************/

// Describes particular FB configuration
struct FXGLVisual::FXGLSpecs {
  int redsize;
  int greensize;
  int bluesize;
  int alphasize;
  int depthsize;
  int stencilsize;
  int accumredsize;
  int accumgreensize;
  int accumbluesize;
  int accumalphasize;
  int doublebuffer;
  int stereobuffer;
  int multisamples;
  int composition;
  int image;
  int accel;
  int copy;
  };


// Match actual GL depth to desired one
FXint FXGLVisual::matchSpecs(const FXGLSpecs& s){
  FXint match,dred,dgreen,dblue,dalpha,ddepth,dstencil,dsamples,daccred,daccgreen,daccblue,daccalpha;

  // We prefer to get a few MORE bits in RGBA than we asked for
  dred   = s.redsize-redSize;     if(dred<0)   dred   *= -100;
  dgreen = s.greensize-greenSize; if(dgreen<0) dgreen *= -100;
  dblue  = s.bluesize-blueSize;   if(dblue<0)  dblue  *= -100;
  dalpha = s.alphasize-alphaSize; if(dalpha<0) dalpha *= -100;

  // Prefer better Z than asked, but colors more important
  ddepth = s.depthsize-depthSize; if(ddepth<0) ddepth *= -10;

  // We care about colors and Z depth more than stencil depth
  dstencil = s.stencilsize-stencilSize; if(dstencil<0) dstencil *= -1;

  // Accumulation buffers
  daccred=s.accumredsize-accumRedSize;       if(daccred<0)   daccred   *= -1;
  daccgreen=s.accumgreensize-accumGreenSize; if(daccgreen<0) daccgreen *= -1;
  daccblue=s.accumbluesize-accumBlueSize;    if(daccblue<0)  daccblue  *= -1;
  daccalpha=s.accumalphasize-accumAlphaSize; if(daccalpha<0) daccalpha *= -1;

  // Want the best colors, of course
  match=dred+dgreen+dblue+dalpha;

  // Accumulation buffers
  match+=daccred+daccgreen+daccblue+daccalpha;

  // Hardware accelerated is normally a plus
  if(!s.accel && !(flags&VISUAL_NO_ACCEL)){
    match+=10000;
    }

  // Extra penalty for no alpha if we asked for alpha, but no
  // penalty at all if there is alpha and we didn't ask for it.
  if(alphaSize>0){
    if(s.alphasize<1) match+=100000;
    }

  // Wanted Z-buffer
  if(depthSize>0){
    if(s.depthsize<1) match+=10000000;
    else match+=ddepth;
    }
  else{
    if(s.depthsize>0) match+=10000000;
    }

  // Stencil buffers desired
  if(stencilSize>0){
    if(s.stencilsize<1) match+=10000;
    else match+=dstencil;
    }
  else{
    if(s.stencilsize>0) match+=1;
    }

  // Multisamples
  if(multiSamples>0){
    dsamples=s.multisamples-multiSamples;
    if(dsamples<0) dsamples*=-10;
    match+=dsamples;
    }
  else{
    if(s.multisamples>0) match+=100;
    }

  // Double buffering also quite strongly preferred
  if(flags&VISUAL_DOUBLE_BUFFER){
    if(!s.doublebuffer) match+=1000000;
    }
  else{
    if(s.doublebuffer) match+=1000000;
    }

  // Stereo not so important
  if(flags&VISUAL_STEREO){
    if(!s.stereobuffer) match+=10000;
    }
  else{
    if(s.stereobuffer) match+=10000;
    }

  // Swap copies also important
  if(flags&VISUAL_SWAP_COPY){
    if(!s.copy) match+=10000000;
    }

  // Composition support would be nice to have
  if(!s.composition) match+=100;

  // Off-screen drawing would be nice
  if(!s.image) match+=1;

  return match;
  }


#if defined(WIN32) ///////////////// WIN32 //////////////////////////////////////

#ifndef PFD_SUPPORT_COMPOSITION
#define PFD_SUPPORT_COMPOSITION         0x00008000
#endif

#ifdef HAVE_GL_H

// Palette struct
struct LOGPALETTE256 {
  WORD         palVersion;
  WORD         palNumEntries;
  PALETTEENTRY palPalEntry[257];
  };


// System colors to match against
static FXuchar defSysClr[20][3] = {
    {  0,  0,  0},
    {128,  0,  0},
    {  0,128,  0},
    {128,128,  0},
    {  0,  0,128},
    {128,  0,128},
    {  0,128,128},
    {192,192,192},

    {192,220,192},
    {166,202,240},
    {255,251,240},
    {160,160,164},

    {128,128,128},
    {255,  0,  0},
    {  0,255,  0},
    {255,255,  0},
    {  0,  0,255},
    {255,  0,255},
    {  0,255,255},
    {255,255,255}
    };

static int defaultOverride[13] = {
  0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91
  };


// Make palette
static HPALETTE makeOpenGLPalette(PIXELFORMATDESCRIPTOR* info){
  LOGPALETTE256 palette;
  int num,i,j,rr,gg,bb;
  int rmax,gmax,bmax;
  HPALETTE hPalette;

  // Size of palette array
  num=1<<((PIXELFORMATDESCRIPTOR*)info)->cColorBits;

  FXASSERT(num<=256);

  // Maximum values each color
  rmax=(1 << info->cRedBits)-1;
  gmax=(1 << info->cGreenBits)-1;
  bmax=(1 << info->cBlueBits)-1;

  // Build palette
  for(rr=0; rr<=rmax; rr++){
    for(gg=0; gg<=gmax; gg++){
      for(bb=0; bb<=bmax; bb++){
        i = (rr << info->cRedShift) | (gg << info->cGreenShift) | (bb << info->cBlueShift);
        palette.palPalEntry[i].peRed = (255*rr)/rmax;
        palette.palPalEntry[i].peGreen = (255*gg)/gmax;
        palette.palPalEntry[i].peBlue = (255*bb)/bmax;
        palette.palPalEntry[i].peFlags = PC_NOCOLLAPSE;
        }
      }
    }

  // For 8-bit palette
  if((info->cColorBits==8) && (info->cRedBits==3) && (info->cRedShift==0) && (info->cGreenBits==3) && (info->cGreenShift==3) && (info->cBlueBits==2) && (info->cBlueShift==6)){
    for(j=1; j<=12; j++){
      palette.palPalEntry[defaultOverride[j]].peRed=defSysClr[j][0];
      palette.palPalEntry[defaultOverride[j]].peGreen=defSysClr[j][1];
      palette.palPalEntry[defaultOverride[j]].peBlue=defSysClr[j][1];
      palette.palPalEntry[defaultOverride[j]].peFlags=0;
      }
    }

  // Fill in the rest
  palette.palVersion=0x300;
  palette.palNumEntries=num;

  // Make palette
  hPalette=CreatePalette((const LOGPALETTE*)&palette);

  return hPalette;
  }

#endif


// Initialize
void FXGLVisual::create(){
  if(!xid){
    if(getApp()->isInitialized()){
      FXTRACE((100,"%s::create %p\n",getClassName(),this));
#ifdef HAVE_GL_H
      PIXELFORMATDESCRIPTOR pfd;
      int bestmatch=1000000000;
      int best=-1;
      int match;
      int npf;
      HDC hdc;

      // Get some window handle
      hdc=GetDC(GetDesktopWindow());

      // Get number of supported pixel formats
      pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR);
      pfd.nVersion=1;

      // Get number of supported pixel formats
      if(0<(npf=DescribePixelFormat(hdc,1,sizeof(PIXELFORMATDESCRIPTOR),&pfd))){

        FXTRACE((140,"Found %d OpenGL configs\n",npf));

        // Try to find the best
        for(int v=1; v<=npf; v++){
          FXGLSpecs specs;

          // Get info about this visual
          DescribePixelFormat(hdc,v,sizeof(PIXELFORMATDESCRIPTOR),&pfd);

          // Make sure this visual is valid
//          if(ChoosePixelFormat(hdc,&pfd)!=v) continue;

          // Get supported render type; we don't want index mode
          if(pfd.iPixelType==PFD_TYPE_COLORINDEX) continue;

          // OpenGL support is required
          if(!(pfd.dwFlags&PFD_SUPPORT_OPENGL)) continue;

          // Draw to window is required
          if(!(pfd.dwFlags&PFD_DRAW_TO_WINDOW)) continue;

          // Get planes
          specs.redsize=pfd.cRedBits;
          specs.greensize=pfd.cGreenBits;
          specs.bluesize=pfd.cBlueBits;
          specs.alphasize=pfd.cAlphaBits;
          specs.depthsize=pfd.cDepthBits;
          specs.stencilsize=pfd.cStencilBits;
          specs.accumredsize=pfd.cAccumRedBits;
          specs.accumgreensize=pfd.cAccumGreenBits;
          specs.accumbluesize=pfd.cAccumBlueBits;
          specs.accumalphasize=pfd.cAccumAlphaBits;

          // Double buffer capable
          specs.doublebuffer=(pfd.dwFlags&PFD_DOUBLEBUFFER)!=0;

          // Stereo capable
          specs.stereobuffer=(pfd.dwFlags&PFD_STEREO)!=0;

          // Multisample
          specs.multisamples=0;

          // Windows Vista and Windows 7 composition support
          specs.composition=(pfd.dwFlags&PFD_SUPPORT_COMPOSITION)!=0;

          // Bitmap drawing would be nice
          specs.image=(pfd.dwFlags&PFD_DRAW_TO_BITMAP)!=0;

          // Hardware accelerated format
          specs.accel=(pfd.dwFlags&PFD_GENERIC_FORMAT)==0;

          // Swap buffers by copying
          specs.copy=(pfd.dwFlags&PFD_SWAP_COPY)!=0;

          // Match specs
          match=matchSpecs(specs);

          // Trace
          FXTRACE((150,"Config: #%d: match=%d\n",v,match));
          FXTRACE((150,"  drawables  = %s%s\n",(pfd.dwFlags&PFD_DRAW_TO_WINDOW)?"PFD_DRAW_TO_WINDOW ":"",(pfd.dwFlags&PFD_DRAW_TO_BITMAP)?"PFD_DRAW_TO_BITMAP ":""));
          FXTRACE((150,"  render     = %s\n",(pfd.iPixelType==PFD_TYPE_COLORINDEX)?"PFD_TYPE_COLORINDEX":(pfd.iPixelType==PFD_TYPE_RGBA)?"PFD_TYPE_RGBA":""));
          FXTRACE((150,"  red size   = %d\n",specs.redsize));
          FXTRACE((150,"  green size = %d\n",specs.greensize));
          FXTRACE((150,"  blue size  = %d\n",specs.bluesize));
          FXTRACE((150,"  alpha size = %d\n",specs.alphasize));
          FXTRACE((150,"  depth size = %d\n",specs.depthsize));
          FXTRACE((150,"  stencil    = %d\n",specs.stencilsize));
          FXTRACE((150,"  acc red    = %d\n",specs.accumredsize));
          FXTRACE((150,"  acc green  = %d\n",specs.accumgreensize));
          FXTRACE((150,"  acc blue   = %d\n",specs.accumbluesize));
          FXTRACE((150,"  acc alpha  = %d\n",specs.accumalphasize));
          FXTRACE((150,"  double buf = %d\n",specs.doublebuffer));
          FXTRACE((150,"  stereo buf = %d\n",specs.stereobuffer));
          FXTRACE((150,"  samples    = %d\n",specs.multisamples));
          FXTRACE((150,"  accel      = %d\n",specs.accel));

          // May the best visual win
          if(match<=bestmatch){
            actualRedSize=specs.redsize;
            actualGreenSize=specs.greensize;
            actualBlueSize=specs.bluesize;
            actualAlphaSize=specs.alphasize;
            actualDepthSize=specs.depthsize;
            actualStencilSize=specs.stencilsize;
            actualMultiSamples=specs.multisamples;
            actualAccumRedSize=specs.accumredsize;
            actualAccumGreenSize=specs.accumgreensize;
            actualAccumBlueSize=specs.accumbluesize;
            actualAccumAlphaSize=specs.accumalphasize;
            doubleBuffer=specs.doublebuffer;
            stereoBuffer=specs.stereobuffer;
            accelerated=specs.accel;
            copying=specs.copy;
            bestmatch=match;
            best=v;
            }
          }
        }

      // Hopefully, we got one
      if(0<=best){
        FXTRACE((140,"Best Config: #%d: match=%d\n",best,bestmatch));

        // Fill in visual data
        depth=actualRedSize+actualGreenSize+actualBlueSize;
        numred=(1<<actualRedSize);
        numgreen=(1<<actualGreenSize);
        numblue=(1<<actualBlueSize);
        numcolors=numred*numgreen*numblue;

        // Make a palette for it if needed
        if(pfd.dwFlags&PFD_NEED_PALETTE){
          colormap=makeOpenGLPalette(&pfd);
          freemap=true;
          }

        // Remember best config
        xid=(void*)(FXival)best;
        }

      // Done with that window
      ::ReleaseDC(GetDesktopWindow(),hdc);

      // Make non-current
      wglMakeCurrent(nullptr,nullptr);
#endif
      }

    // Test if successful
    if(!xid){
      throw FXWindowException("unable to create GL context.");
      }
    }
  }


#else //////////////////////////////// X11 OLD //////////////////////////////////


#ifndef GLX_ARB_multisample
#define GLX_SAMPLE_BUFFERS_ARB             100000
#define GLX_SAMPLES_ARB                    100001
#endif
#ifndef GLX_EXT_visual_rating
#define GLX_VISUAL_CAVEAT_EXT              0x20
#define GLX_SLOW_VISUAL_EXT                0x8001
#define GLX_NON_CONFORMANT_VISUAL_EXT      0x800D
#endif


// Initialize
void FXGLVisual::create(){
  if(!xid){
    if(getApp()->isInitialized()){
      FXTRACE((100,"%s::create %p\n",getClassName(),this));
#ifdef HAVE_GL_H
      int majoropcode,errorbase,eventbase;

      // Assume the default
      visual=DefaultVisual((Display*)getApp()->getDisplay(),DefaultScreen((Display*)getApp()->getDisplay()));
      depth=DefaultDepth((Display*)getApp()->getDisplay(),DefaultScreen((Display*)getApp()->getDisplay()));

      // OpenGL is available if we're talking to an OpenGL-capable X-Server
      if(XQueryExtension((Display*)getApp()->getDisplay(),"GLX",&majoropcode,&errorbase,&eventbase)){
        if(glXQueryExtension((Display*)getApp()->getDisplay(),&errorbase,&eventbase)){
          int major,minor;

          // Try get OpenGL version info
          if(glXQueryVersion((Display*)getApp()->getDisplay(),&major,&minor)){
            XVisualInfo vitemplate,*vi; int nvi;

            // Report version found
            FXTRACE((100,"Found GLX version: %d.%d (Major: %d, Error: %d, Event: %d)\n",major,minor,majoropcode,errorbase,eventbase));

            // Scan for all visuals of given screen
            vitemplate.screen=DefaultScreen((Display*)getApp()->getDisplay());
            vi=XGetVisualInfo((Display*)getApp()->getDisplay(),VisualScreenMask,&vitemplate,&nvi);
            if(vi){
              int defvisualid=XVisualIDFromVisual(DefaultVisual((Display*)getApp()->getDisplay(),DefaultScreen((Display*)getApp()->getDisplay())));
              int samplebuffers=0;
              int bestmatch=1000000000;
              int best=-1;
              int value;
              int match;

              FXTRACE((150,"Found %d configs\n",nvi));
              FXTRACE((150,"Default visualid=0x%02x\n",defvisualid));

              // Find the best one
              for(int v=0; v<nvi; v++){
                FXGLSpecs specs;

                // GL support is requested
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_USE_GL,&value)!=Success || (value==0)) continue;

                // Get supported drawable targets
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_RGBA,&value)!=Success || (value==0)) continue;

                // Get overlay level
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_LEVEL,&value)!=Success || (value!=0)) continue;

                // Get plane depths
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_RED_SIZE,&specs.redsize)!=Success) continue;
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_GREEN_SIZE,&specs.greensize)!=Success) continue;
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_BLUE_SIZE,&specs.bluesize)!=Success) continue;
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_ALPHA_SIZE,&specs.alphasize)!=Success) continue;
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_DEPTH_SIZE,&specs.depthsize)!=Success) continue;
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_STENCIL_SIZE,&specs.stencilsize)!=Success) continue;
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_ACCUM_RED_SIZE,&specs.accumredsize)!=Success) continue;
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_ACCUM_GREEN_SIZE,&specs.accumgreensize)!=Success) continue;
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_ACCUM_BLUE_SIZE,&specs.accumbluesize)!=Success) continue;
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_ACCUM_ALPHA_SIZE,&specs.accumalphasize)!=Success) continue;

                // Get stereo and double buffer support
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_DOUBLEBUFFER,&specs.doublebuffer)!=Success) continue;
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_STEREO,&specs.stereobuffer)!=Success) continue;

                // Get multisample support (if we don't succeed, set it to zero)
                specs.multisamples=0;
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_SAMPLE_BUFFERS_ARB,&samplebuffers)==Success && samplebuffers){
                  if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_SAMPLES_ARB,&specs.multisamples)!=Success) continue;
                  }

                // Check if accelerated or not (assume yes)
                if(glXGetConfig((Display*)getApp()->getDisplay(),&vi[v],GLX_VISUAL_CAVEAT_EXT,&specs.accel)==Success){
                  specs.accel=(specs.accel!=GLX_SLOW_VISUAL_EXT);        // Penalize if any caveats present
                  }
                else{
                  specs.accel=1;
                  }

                // Composition and swap copy
                specs.composition=false;
                specs.image=false;
                specs.copy=false;

                // Match specs
                match=matchSpecs(specs);

                // Trace
                FXTRACE((150,"Config: #%d: match=%d\n",v,match));
                FXTRACE((150,"  visualid   = 0x%02lx\n",vi[v].visualid));
                FXTRACE((150,"  red size   = %d\n",specs.redsize));
                FXTRACE((150,"  green size = %d\n",specs.greensize));
                FXTRACE((150,"  blue size  = %d\n",specs.bluesize));
                FXTRACE((150,"  alpha size = %d\n",specs.alphasize));
                FXTRACE((150,"  depth size = %d\n",specs.depthsize));
                FXTRACE((150,"  stencil    = %d\n",specs.stencilsize));
                FXTRACE((150,"  acc red    = %d\n",specs.accumredsize));
                FXTRACE((150,"  acc green  = %d\n",specs.accumgreensize));
                FXTRACE((150,"  acc blue   = %d\n",specs.accumbluesize));
                FXTRACE((150,"  acc alpha  = %d\n",specs.accumalphasize));
                FXTRACE((150,"  double buf = %d\n",specs.doublebuffer));
                FXTRACE((150,"  stereo buf = %d\n",specs.stereobuffer));
                FXTRACE((150,"  samples    = %d\n",specs.multisamples));
                FXTRACE((150,"  accel      = %d\n",specs.accel));

                // May the best config win
                if(match<=bestmatch){

                  // All other things being equal, we prefer default visual!
                  if((match<bestmatch) || (vi[v].visualid==defvisualid)){
                    actualRedSize=specs.redsize;
                    actualGreenSize=specs.greensize;
                    actualBlueSize=specs.bluesize;
                    actualAlphaSize=specs.alphasize;
                    actualDepthSize=specs.depthsize;
                    actualStencilSize=specs.stencilsize;
                    actualMultiSamples=specs.multisamples;
                    actualAccumRedSize=specs.accumredsize;
                    actualAccumGreenSize=specs.accumgreensize;
                    actualAccumBlueSize=specs.accumbluesize;
                    actualAccumAlphaSize=specs.accumalphasize;
                    doubleBuffer=specs.doublebuffer;
                    stereoBuffer=specs.stereobuffer;
                    accelerated=specs.accel;
                    copying=specs.copy;
                    bestmatch=match;
                    best=v;
                    }
                  }
                }

              // We should have one now
              if(0<=best){
                FXTRACE((140,"Best Config: #%d: match=%d\n",best,bestmatch));

                // Remember visual, depth, visualid
                visual=vi[best].visual;
                depth=vi[best].depth;

                // Initialize colormap
                setupcolormap();

                // Make GC's for this visual
                scrollgc=setupgc(true);
                gc=setupgc(false);

                // Remember best config
                xid=((Visual*)visual)->visualid;
                }

              // Free visual info
              XFree((char*)vi);
              }
            }
          }
        }
#endif
      }

    // Test if successful
    if(!xid){
      throw FXWindowException("no matching GL configuration.");
      }
    }
  }


#endif //////////////////////////////////////////////////////////////////////////


// Detach visual
void FXGLVisual::detach(){
#ifdef HAVE_GL_H
  if(xid){
    FXTRACE((100,"%s::detach %p\n",getClassName(),this));
    colormap=0;
    freemap=false;
    xid=0;
    }
#endif
  }


// Destroy visual
void FXGLVisual::destroy(){
#ifdef HAVE_GL_H
  if(xid){
    if(getApp()->isInitialized()){
      FXTRACE((100,"%s::destroy %p\n",getClassName(),this));
#ifdef WIN32
      if(colormap){ DeleteObject(colormap); }
#else
      if(freemap){ XFreeColormap((Display*)getApp()->getDisplay(),colormap); }
      if(scrollgc){ XFreeGC((Display*)getApp()->getDisplay(),(GC)scrollgc); }
      if(gc){ XFreeGC((Display*)getApp()->getDisplay(),(GC)gc); }
#endif
      }
#ifndef WIN32
    scrollgc=0;
    gc=0;
#endif
    colormap=0;
    freemap=false;
    xid=0;
    }
#endif
  }


// Save to stream
void FXGLVisual::save(FXStream& store) const {
  FXVisual::save(store);
  store << redSize;
  store << greenSize;
  store << blueSize;
  store << alphaSize;
  store << depthSize;
  store << stencilSize;
  store << multiSamples;
  store << accumRedSize;
  store << accumGreenSize;
  store << accumBlueSize;
  store << accumAlphaSize;
  }


// Load from stream
void FXGLVisual::load(FXStream& store){
  FXVisual::load(store);
  store >> redSize;
  store >> greenSize;
  store >> blueSize;
  store >> alphaSize;
  store >> depthSize;
  store >> stencilSize;
  store >> multiSamples;
  store >> accumRedSize;
  store >> accumGreenSize;
  store >> accumBlueSize;
  store >> accumAlphaSize;
  }


// Destroy
FXGLVisual::~FXGLVisual(){
  FXTRACE((100,"FXGLVisual::~FXGLVisual %p\n",this));
  destroy();
  }


}