summaryrefslogtreecommitdiffstats
path: root/src/opengl/qgl_mac.mm
blob: 66fe7d33ed492eafebfe69cac422dbd00e731a44 (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
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtOpenGL module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qgl.h"

// There are functions that are deprecated in 10.5, but really there's no way around them
// for Carbon, so just undefine them.
#undef DEPRECATED_ATTRIBUTE
#define DEPRECATED_ATTRIBUTE
#if defined(Q_WS_MAC)
#ifndef QT_MAC_USE_COCOA
#ifdef qDebug
#    undef qDebug
#    include <AGL/agl.h>
#    include <AGL/aglRenderers.h>
#    include <OpenGL/gl.h>
#    ifdef QT_NO_DEBUG
#        define qDebug qt_noop(),1?(void)0:qDebug
#    endif
#else
#    include <AGL/agl.h>
#    include <AGL/aglRenderers.h>
#    include <OpenGL/gl.h>
#endif
#else
#include <private/qcocoaview_mac_p.h>
#endif


#include <OpenGL/gl.h>
#include <CoreServices/CoreServices.h>
#include <private/qfont_p.h>
#include <private/qfontengine_p.h>
#include <private/qgl_p.h>
#include <private/qpaintengine_opengl_p.h>
#include <private/qt_mac_p.h>
#include <qpixmap.h>
#include <qtimer.h>
#include <qapplication.h>
#include <qstack.h>
#include <qdesktopwidget.h>
#include <qdebug.h>

QT_BEGIN_NAMESPACE
#ifdef QT_MAC_USE_COCOA
QT_END_NAMESPACE

QT_FORWARD_DECLARE_CLASS(QWidget)
QT_FORWARD_DECLARE_CLASS(QWidgetPrivate)
QT_FORWARD_DECLARE_CLASS(QGLWidgetPrivate)

QT_BEGIN_NAMESPACE

void *qt_current_nsopengl_context()
{
    return [NSOpenGLContext currentContext];
}

static GLint attribValue(NSOpenGLPixelFormat *fmt, NSOpenGLPixelFormatAttribute attrib)
{
    GLint res;
    [fmt getValues:&res forAttribute:attrib forVirtualScreen:0];
    return res;
}

static int def(int val, int defVal)
{
    return val != -1 ? val : defVal;
}
#else
QRegion qt_mac_get_widget_rgn(const QWidget *widget);
#endif

extern quint32 *qt_mac_pixmap_get_base(const QPixmap *);
extern int qt_mac_pixmap_get_bytes_per_line(const QPixmap *);
extern RgnHandle qt_mac_get_rgn(); //qregion_mac.cpp
extern void qt_mac_dispose_rgn(RgnHandle); //qregion_mac.cpp
extern QRegion qt_mac_convert_mac_region(RgnHandle); //qregion_mac.cpp
extern void qt_mac_to_pascal_string(QString s, Str255 str, TextEncoding encoding=0, int len=-1);  //qglobal.cpp

/*
    QGLTemporaryContext implementation
*/

class QGLTemporaryContextPrivate
{
public:
#ifndef QT_MAC_USE_COCOA
    AGLContext ctx;
#else
    NSOpenGLContext *ctx;
#endif
};

QGLTemporaryContext::QGLTemporaryContext(bool, QWidget *)
    : d(new QGLTemporaryContextPrivate)
{
    d->ctx = 0;
#ifndef QT_MAC_USE_COCOA
    GLint attribs[] = {AGL_RGBA, AGL_NONE};
    AGLPixelFormat fmt = aglChoosePixelFormat(0, 0, attribs);
    if (!fmt) {
        qDebug("QGLTemporaryContext: Couldn't find any RGB visuals");
        return;
    }
    d->ctx = aglCreateContext(fmt, 0);
    if (!d->ctx)
        qDebug("QGLTemporaryContext: Unable to create context");
    else
        aglSetCurrentContext(d->ctx);
    aglDestroyPixelFormat(fmt);
#else
    QMacCocoaAutoReleasePool pool;
    NSOpenGLPixelFormatAttribute attribs[] = { 0 };
    NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
    if (!fmt) {
        qWarning("QGLTemporaryContext: Cannot find any visuals");
        return;
    }

    d->ctx = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:0];
    if (!d->ctx)
        qWarning("QGLTemporaryContext: Cannot create context");
    else
        [d->ctx makeCurrentContext];
    [fmt release];
#endif
}

QGLTemporaryContext::~QGLTemporaryContext()
{
    if (d->ctx) {
#ifndef QT_MAC_USE_COCOA
        aglSetCurrentContext(0);
        aglDestroyContext(d->ctx);
#else
        [NSOpenGLContext clearCurrentContext];
        [d->ctx release];
#endif
    }
}

bool QGLFormat::hasOpenGL()
{
    return true;
}

bool QGLFormat::hasOpenGLOverlays()
{
    return false;
}

bool QGLContext::chooseContext(const QGLContext *shareContext)
{
    QMacCocoaAutoReleasePool pool;
    Q_D(QGLContext);
    d->cx = 0;
    d->vi = chooseMacVisual(0);
    if (!d->vi)
        return false;

#ifndef QT_MAC_USE_COCOA
    AGLPixelFormat fmt = (AGLPixelFormat)d->vi;
    GLint res;
    aglDescribePixelFormat(fmt, AGL_LEVEL, &res);
    d->glFormat.setPlane(res);
    if (deviceIsPixmap())
        res = 0;
    else
        aglDescribePixelFormat(fmt, AGL_DOUBLEBUFFER, &res);
    d->glFormat.setDoubleBuffer(res);
    aglDescribePixelFormat(fmt, AGL_DEPTH_SIZE, &res);
    d->glFormat.setDepth(res);
    if (d->glFormat.depth())
        d->glFormat.setDepthBufferSize(res);
    aglDescribePixelFormat(fmt, AGL_RGBA, &res);
    d->glFormat.setRgba(res);
    aglDescribePixelFormat(fmt, AGL_RED_SIZE, &res);
    d->glFormat.setRedBufferSize(res);
    aglDescribePixelFormat(fmt, AGL_GREEN_SIZE, &res);
    d->glFormat.setGreenBufferSize(res);
    aglDescribePixelFormat(fmt, AGL_BLUE_SIZE, &res);
    d->glFormat.setBlueBufferSize(res);
    aglDescribePixelFormat(fmt, AGL_ALPHA_SIZE, &res);
    d->glFormat.setAlpha(res);
    if (d->glFormat.alpha())
        d->glFormat.setAlphaBufferSize(res);
    aglDescribePixelFormat(fmt, AGL_ACCUM_RED_SIZE, &res);
    // Bug in Apple OpenGL (rdr://5015603), when we don't have an accumulation
    // buffer, it still claims that we have a 16-bit one (which is pretty rare).
    // So, we just assume we can never have a buffer that small.
    d->glFormat.setAccum(res > 5);
    if (d->glFormat.accum())
        d->glFormat.setAccumBufferSize(res);
    aglDescribePixelFormat(fmt, AGL_STENCIL_SIZE, &res);
    d->glFormat.setStencil(res);
    if (d->glFormat.stencil())
        d->glFormat.setStencilBufferSize(res);
    aglDescribePixelFormat(fmt, AGL_STEREO, &res);
    d->glFormat.setStereo(res);
    aglDescribePixelFormat(fmt, AGL_SAMPLE_BUFFERS_ARB, &res);
    d->glFormat.setSampleBuffers(res);
    if (d->glFormat.sampleBuffers()) {
        aglDescribePixelFormat(fmt, AGL_SAMPLES_ARB, &res);
        d->glFormat.setSamples(res);
    }
#else
    NSOpenGLPixelFormat *fmt = static_cast<NSOpenGLPixelFormat *>(d->vi);

    d->glFormat = QGLFormat();

    // ### make sure to reset other options
    d->glFormat.setDoubleBuffer(attribValue(fmt, NSOpenGLPFADoubleBuffer));

    int depthSize = attribValue(fmt, NSOpenGLPFADepthSize);
    d->glFormat.setDepth(depthSize > 0);
    if (depthSize > 0)
        d->glFormat.setDepthBufferSize(depthSize);

    int alphaSize = attribValue(fmt, NSOpenGLPFAAlphaSize);
    d->glFormat.setAlpha(alphaSize > 0);
    if (alphaSize > 0)
        d->glFormat.setAlphaBufferSize(alphaSize);

    int accumSize = attribValue(fmt, NSOpenGLPFAAccumSize);
    d->glFormat.setAccum(accumSize > 0);
    if (accumSize > 0)
        d->glFormat.setAccumBufferSize(accumSize);

    int stencilSize = attribValue(fmt, NSOpenGLPFAStencilSize);
    d->glFormat.setStencil(stencilSize > 0);
    if (stencilSize > 0)
        d->glFormat.setStencilBufferSize(stencilSize);

    d->glFormat.setStereo(attribValue(fmt, NSOpenGLPFAStereo));

    int sampleBuffers = attribValue(fmt, NSOpenGLPFASampleBuffers);
    d->glFormat.setSampleBuffers(sampleBuffers);
    if (sampleBuffers > 0)
        d->glFormat.setSamples(attribValue(fmt, NSOpenGLPFASamples));
#endif
    if (shareContext && (!shareContext->isValid() || !shareContext->d_func()->cx)) {
        qWarning("QGLContext::chooseContext: Cannot share with invalid context");
        shareContext = 0;
    }

    // sharing between rgba and color-index will give wrong colors
    if (shareContext && (format().rgba() != shareContext->format().rgba()))
        shareContext = 0;

#ifndef QT_MAC_USE_COCOA
    AGLContext ctx = aglCreateContext(fmt, (AGLContext) (shareContext ? shareContext->d_func()->cx : 0));
#else
    NSOpenGLContext *ctx = [[NSOpenGLContext alloc] initWithFormat:fmt
        shareContext:(shareContext ? static_cast<NSOpenGLContext *>(shareContext->d_func()->cx)
                                   : 0)];
#endif
    if (!ctx) {
#ifndef QT_MAC_USE_COCOA
        GLenum err = aglGetError();
        if (err == AGL_BAD_MATCH || err == AGL_BAD_CONTEXT) {
            if (shareContext && shareContext->d_func()->cx) {
                qWarning("QGLContext::chooseContext(): Context sharing mismatch!");
                if (!(ctx = aglCreateContext(fmt, 0)))
                    return false;
                shareContext = 0;
            }
        }
#else
        if (shareContext) {
            ctx = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:0];
            if (ctx) {
                qWarning("QGLContext::chooseContext: Context sharing mismatch");
                shareContext = 0;
            }
        }
#endif
        if (!ctx) {
            qWarning("QGLContext::chooseContext: Unable to create QGLContext");
            return false;
        }
    }
    d->cx = ctx;
    if (shareContext && shareContext->d_func()->cx) {
        QGLContext *share = const_cast<QGLContext *>(shareContext);
        d->sharing = true;
        share->d_func()->sharing = true;
    }
    if (deviceIsPixmap())
        updatePaintDevice();

    // vblank syncing
    GLint interval = d->reqFormat.swapInterval();
    if (interval != -1) {
#ifndef QT_MAC_USE_COCOA
        aglSetInteger((AGLContext)d->cx, AGL_SWAP_INTERVAL, &interval);
        if (interval != 0)
            aglEnable((AGLContext)d->cx, AGL_SWAP_INTERVAL);
        else
            aglDisable((AGLContext)d->cx, AGL_SWAP_INTERVAL);
#else
        [ctx setValues:&interval forParameter:NSOpenGLCPSwapInterval];
#endif
    }
#ifndef QT_MAC_USE_COCOA
    aglGetInteger((AGLContext)d->cx, AGL_SWAP_INTERVAL, &interval);
#else
    [ctx getValues:&interval forParameter:NSOpenGLCPSwapInterval];
#endif
    d->glFormat.setSwapInterval(interval);
    return true;
}

void *QGLContextPrivate::tryFormat(const QGLFormat &format)
{
    static const int Max = 40;
#ifndef QT_MAC_USE_COCOA
    GLint attribs[Max], cnt = 0;
    bool device_is_pixmap = (paintDevice->devType() == QInternal::Pixmap);

    attribs[cnt++] = AGL_RGBA;
    attribs[cnt++] = AGL_BUFFER_SIZE;
    attribs[cnt++] = device_is_pixmap ? static_cast<QPixmap *>(paintDevice)->depth() : 32;
    attribs[cnt++] = AGL_LEVEL;
    attribs[cnt++] = format.plane();

    if (format.redBufferSize() != -1) {
        attribs[cnt++] = AGL_RED_SIZE;
        attribs[cnt++] = format.redBufferSize();
    }
    if (format.greenBufferSize() != -1) {
        attribs[cnt++] = AGL_GREEN_SIZE;
        attribs[cnt++] = format.greenBufferSize();
    }
    if (format.blueBufferSize() != -1) {
        attribs[cnt++] = AGL_BLUE_SIZE;
        attribs[cnt++] = format.blueBufferSize();
    }
    if (device_is_pixmap) {
        attribs[cnt++] = AGL_PIXEL_SIZE;
        attribs[cnt++] = static_cast<QPixmap *>(paintDevice)->depth();
        attribs[cnt++] = AGL_OFFSCREEN;
        if (!format.alpha()) {
            attribs[cnt++] = AGL_ALPHA_SIZE;
            attribs[cnt++] = 8;
        }
    } else {
        if (format.doubleBuffer())
            attribs[cnt++] = AGL_DOUBLEBUFFER;
    }

    if (format.stereo())
        attribs[cnt++] = AGL_STEREO;
    if (format.alpha()) {
        attribs[cnt++] = AGL_ALPHA_SIZE;
        attribs[cnt++] = format.alphaBufferSize() == -1 ? 8 : format.alphaBufferSize();
    }
    if (format.stencil()) {
        attribs[cnt++] = AGL_STENCIL_SIZE;
        attribs[cnt++] = format.stencilBufferSize() == -1 ? 8 : format.stencilBufferSize();
    }
    if (format.depth()) {
        attribs[cnt++] = AGL_DEPTH_SIZE;
        attribs[cnt++] = format.depthBufferSize() == -1 ? 32 : format.depthBufferSize();
    }
    if (format.accum()) {
        attribs[cnt++] = AGL_ACCUM_RED_SIZE;
        attribs[cnt++] = format.accumBufferSize() == -1 ? 1 : format.accumBufferSize();
        attribs[cnt++] = AGL_ACCUM_BLUE_SIZE;
        attribs[cnt++] = format.accumBufferSize() == -1 ? 1 : format.accumBufferSize();
        attribs[cnt++] = AGL_ACCUM_GREEN_SIZE;
        attribs[cnt++] = format.accumBufferSize() == -1 ? 1 : format.accumBufferSize();
        attribs[cnt++] = AGL_ACCUM_ALPHA_SIZE;
        attribs[cnt++] = format.accumBufferSize() == -1 ? 1 : format.accumBufferSize();
    }
    if (format.sampleBuffers()) {
        attribs[cnt++] = AGL_SAMPLE_BUFFERS_ARB;
        attribs[cnt++] = 1;
        attribs[cnt++] = AGL_SAMPLES_ARB;
        attribs[cnt++] = format.samples() == -1 ? 4 : format.samples();
    }

    attribs[cnt] = AGL_NONE;
    Q_ASSERT(cnt < Max);
    return aglChoosePixelFormat(0, 0, attribs);
#else
    NSOpenGLPixelFormatAttribute attribs[Max];
    int cnt = 0;
    int devType = paintDevice->devType();
    bool device_is_pixmap = (devType == QInternal::Pixmap);
    int depth = device_is_pixmap ? static_cast<QPixmap *>(paintDevice)->depth() : 32;

    attribs[cnt++] = NSOpenGLPFAColorSize;
    attribs[cnt++] = depth;

    if (device_is_pixmap) {
        attribs[cnt++] = NSOpenGLPFAOffScreen;
    } else {
        if (format.doubleBuffer())
            attribs[cnt++] = NSOpenGLPFADoubleBuffer;
    }
    if (glFormat.stereo())
        attribs[cnt++] = NSOpenGLPFAStereo;
    if (device_is_pixmap || format.alpha()) {
        attribs[cnt++] = NSOpenGLPFAAlphaSize;
        attribs[cnt++] = def(format.alphaBufferSize(), 8);
    }
    if (format.stencil()) {
        attribs[cnt++] = NSOpenGLPFAStencilSize;
        attribs[cnt++] = def(format.stencilBufferSize(), 8);
    }
    if (format.depth()) {
        attribs[cnt++] = NSOpenGLPFADepthSize;
        attribs[cnt++] = def(format.depthBufferSize(), 32);
    }
    if (format.accum()) {
        attribs[cnt++] = NSOpenGLPFAAccumSize;
        attribs[cnt++] = def(format.accumBufferSize(), 1);
    }
    if (format.sampleBuffers()) {
        attribs[cnt++] = NSOpenGLPFASampleBuffers;
        attribs[cnt++] = 1;
        attribs[cnt++] = NSOpenGLPFASamples;
        attribs[cnt++] = def(format.samples(), 4);
    }

    if (format.directRendering())
        attribs[cnt++] = NSOpenGLPFAAccelerated;

    if (devType == QInternal::Pbuffer)
        attribs[cnt++] = NSOpenGLPFAPixelBuffer;

    attribs[cnt] = 0;
    Q_ASSERT(cnt < Max);
    return [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
#endif
}

void QGLContextPrivate::clearDrawable()
{
    [static_cast<NSOpenGLContext *>(cx) clearDrawable];
}

/*!
    \bold{Mac OS X only:} This virtual function tries to find a visual that
    matches the format, reducing the demands if the original request
    cannot be met.

    The algorithm for reducing the demands of the format is quite
    simple-minded, so override this method in your subclass if your
    application has spcific requirements on visual selection.

    The \a handle argument is always zero and is not used

    \sa chooseContext()
*/

void *QGLContext::chooseMacVisual(GDHandle /* handle */)
{
    Q_D(QGLContext);

    void *fmt = d->tryFormat(d->glFormat);
    if (!fmt && d->glFormat.stereo()) {
        d->glFormat.setStereo(false);
        fmt = d->tryFormat(d->glFormat);
    }
    if (!fmt && d->glFormat.sampleBuffers()) {
        d->glFormat.setSampleBuffers(false);
        fmt = d->tryFormat(d->glFormat);
    }
    if (!fmt)
        qWarning("QGLContext::chooseMacVisual: Unable to choose a pixel format");
    return fmt;
}

void QGLContext::reset()
{
    Q_D(QGLContext);
    if (!d->valid)
        return;
    d->cleanup();
    doneCurrent();
#ifndef QT_MAC_USE_COCOA
    if (d->cx)
        aglDestroyContext((AGLContext)d->cx);
#else
    QMacCocoaAutoReleasePool pool;
    [static_cast<NSOpenGLContext *>(d->cx) release];
#endif
    d->cx = 0;
#ifndef QT_MAC_USE_COCOA
    if (d->vi)
        aglDestroyPixelFormat((AGLPixelFormat)d->vi);
#else
    [static_cast<NSOpenGLPixelFormat *>(d->vi) release];
#endif
    d->vi = 0;
    d->crWin = false;
    d->sharing = false;
    d->valid = false;
    d->transpColor = QColor();
    d->initDone = false;
    QGLContextGroup::removeShare(this);
}

void QGLContext::makeCurrent()
{
    Q_D(QGLContext);

    if (!d->valid) {
        qWarning("QGLContext::makeCurrent: Cannot make invalid context current");
        return;
    }
#ifndef QT_MAC_USE_COCOA
    aglSetCurrentContext((AGLContext)d->cx);
    if (d->update)
        updatePaintDevice();
#else
    [static_cast<NSOpenGLContext *>(d->cx) makeCurrentContext];
#endif
    QGLContextPrivate::setCurrentContext(this);
}

#ifndef QT_MAC_USE_COCOA
/*
    Returns the effective scale factor for a widget. For this value to be
    different than 1, the following must be true:
    - The system scale factor must be greater than 1.
    - The widget window must have WA_MacFrameworkScaled set.
*/
float qt_mac_get_scale_factor(QWidget *widget)
{
    if (!widget | !widget->window())
        return 1;

    if (widget->window()->testAttribute(Qt::WA_MacFrameworkScaled) == false)
        return 1;

    float systemScale = QSysInfo::MacintoshVersion >= QSysInfo::MV_10_4 ? HIGetScaleFactor() : 1;
    if (systemScale == float(1))
        return 1;

    return systemScale;
}
#endif

/*! \internal
*/
void QGLContext::updatePaintDevice()
{
    Q_D(QGLContext);
#ifndef QT_MAC_USE_COCOA
    d->update = false;
    if (d->paintDevice->devType() == QInternal::Widget) {
        //get control information
        QWidget *w = (QWidget *)d->paintDevice;
        HIViewRef hiview = (HIViewRef)w->winId();
        WindowRef window = HIViewGetWindow(hiview);
#ifdef DEBUG_OPENGL_REGION_UPDATE
        static int serial_no_gl = 0;
        qDebug("[%d] %p setting on %s::%s %p/%p [%s]", ++serial_no_gl, w,
                w->metaObject()->className(), w->objectName().toLatin1().constData(),
                hiview, window, w->handle() ? "Inside" : "Outside");
#endif

        //update drawable
        if (0 && w->isWindow() && w->isFullScreen()) {
            aglSetDrawable((AGLContext)d->cx, 0);
            aglSetFullScreen((AGLContext)d->cx, w->width(), w->height(), 0, QApplication::desktop()->screenNumber(w));
            w->hide();
        } else {
            AGLDrawable old_draw = aglGetDrawable((AGLContext)d->cx), new_draw = GetWindowPort(window);
            if (old_draw != new_draw)
                aglSetDrawable((AGLContext)d->cx, new_draw);
        }

        float scale  = qt_mac_get_scale_factor(w);

        if (!w->isWindow()) {
            QRegion clp = qt_mac_get_widget_rgn(w); //get drawable area

#ifdef DEBUG_OPENGL_REGION_UPDATE
            if (clp.isEmpty()) {
                qDebug("  Empty area!");
            } else {
                QVector<QRect> rs = clp.rects();
                for(int i = 0; i < rs.count(); i++)
                    qDebug("  %d %d %d %d", rs[i].x(), rs[i].y(), rs[i].width(), rs[i].height());
            }
#endif
            //update the clip
            if (!aglIsEnabled((AGLContext)d->cx, AGL_BUFFER_RECT))
                aglEnable((AGLContext)d->cx, AGL_BUFFER_RECT);
            if (clp.isEmpty()) {
                GLint offs[4] = { 0, 0, 0, 0 };
                aglSetInteger((AGLContext)d->cx, AGL_BUFFER_RECT, offs);
                if (aglIsEnabled((AGLContext)d->cx, AGL_CLIP_REGION))
                    aglDisable((AGLContext)d->cx, AGL_CLIP_REGION);
            } else {
                HIPoint origin = { 0., 0. };
                HIViewConvertPoint(&origin, HIViewRef(w->winId()), 0);
                const GLint offs[4] = { qRound(origin.x),
                    w->window()->frameGeometry().height() * scale
                        - (qRound(origin.y) + w->height() * scale),
                    w->width() * scale, w->height() * scale};

                RgnHandle region = clp.handle(true);

                if (scale != float(1)) {
                    // Sacle the clip region by the scale factor
                    Rect regionBounds;
                    GetRegionBounds(region, &regionBounds);
                    Rect regionBoundsDest = regionBounds;
                    regionBoundsDest.bottom *= scale;
                    regionBoundsDest.right *= scale;
                    MapRgn(region, &regionBounds, &regionBoundsDest);
                }

                aglSetInteger((AGLContext)d->cx, AGL_BUFFER_RECT, offs);
                aglSetInteger((AGLContext)d->cx, AGL_CLIP_REGION, (const GLint *)region);
                if (!aglIsEnabled((AGLContext)d->cx, AGL_CLIP_REGION))
                    aglEnable((AGLContext)d->cx, AGL_CLIP_REGION);
            }
        } else {
            // Set the buffer rect for top-level gl contexts when scaled.
            if (scale != float(1)) {
                aglEnable((AGLContext)d->cx, AGL_BUFFER_RECT);
                const GLint offs[4] = { 0, 0,  w->width() * scale , w->height() * scale};
                aglSetInteger((AGLContext)d->cx, AGL_BUFFER_RECT, offs);
            }
        }
    } else if (d->paintDevice->devType() == QInternal::Pixmap) {
        QPixmap *pm = reinterpret_cast<QPixmap *>(d->paintDevice);

        unsigned long qdformat = k32ARGBPixelFormat;
        if (QSysInfo::ByteOrder == QSysInfo::LittleEndian)
            qdformat = k32BGRAPixelFormat;
        Rect rect;
        SetRect(&rect, 0, 0, pm->width(), pm->height());

        GWorldPtr gworld;
        NewGWorldFromPtr(&gworld, qdformat, &rect, 0, 0, 0,
                         reinterpret_cast<char *>(qt_mac_pixmap_get_base(pm)), 
                         qt_mac_pixmap_get_bytes_per_line(pm));

        PixMapHandle pixmapHandle = GetGWorldPixMap(gworld);
        aglSetOffScreen(reinterpret_cast<AGLContext>(d->cx), pm->width(), pm->height(),
                        GetPixRowBytes(pixmapHandle), GetPixBaseAddr(pixmapHandle));
    } else {
        qWarning("QGLContext::updatePaintDevice(): Not sure how to render OpenGL on this device!");
    }
    aglUpdateContext((AGLContext)d->cx);

#else
    QMacCocoaAutoReleasePool pool;

    if (d->paintDevice->devType() == QInternal::Widget) {
        //get control information
        QWidget *w = (QWidget *)d->paintDevice;
        NSView *view = qt_mac_nativeview_for(w);

        // ideally we would use QWidget::isVisible(), but we get "invalid drawable" errors
        if (![(NSWindow *)qt_mac_window_for(w) isVisible])
            return;
        if ([static_cast<NSOpenGLContext *>(d->cx) view] != view && ![view isHidden])
            [static_cast<NSOpenGLContext *>(d->cx) setView:view];
    } else if (d->paintDevice->devType() == QInternal::Pixmap) {
        const QPixmap *pm = static_cast<const QPixmap *>(d->paintDevice);
        [static_cast<NSOpenGLContext *>(d->cx) setOffScreen:qt_mac_pixmap_get_base(pm)
                                                      width:pm->width()
                                                     height:pm->height()
                                                   rowbytes:qt_mac_pixmap_get_bytes_per_line(pm)];
    } else {
        qWarning("QGLContext::updatePaintDevice: Not sure how to render OpenGL on this device");
    }
    [static_cast<NSOpenGLContext *>(d->cx) update];
#endif
}

void QGLContext::doneCurrent()
{

    if (
#ifndef QT_MAC_USE_COCOA
        aglGetCurrentContext() != (AGLContext) d_func()->cx
#else
        [NSOpenGLContext currentContext] != d_func()->cx
#endif
       )
        return;

    QGLContextPrivate::setCurrentContext(0);
#ifndef QT_MAC_USE_COCOA
    aglSetCurrentContext(0);
#else
    [NSOpenGLContext clearCurrentContext];
#endif
}

void QGLContext::swapBuffers() const
{
    Q_D(const QGLContext);
    if (!d->valid)
        return;
#ifndef QT_MAC_USE_COCOA
    aglSwapBuffers((AGLContext)d->cx);
#else
    [static_cast<NSOpenGLContext *>(d->cx) flushBuffer];
#endif
}

QColor QGLContext::overlayTransparentColor() const
{
    return QColor(0, 0, 0);                // Invalid color
}

#ifndef QT_MAC_USE_COCOA
static QColor cmap[256];
static bool cmap_init = false;
#endif
uint QGLContext::colorIndex(const QColor &c) const
{
#ifndef QT_MAC_USE_COCOA
    int ret = -1;
    if(!cmap_init) {
        cmap_init = true;
        for(int i = 0; i < 256; i++)
            cmap[i] = QColor();
    } else {
        for(int i = 0; i < 256; i++) {
            if(cmap[i].isValid() && cmap[i] == c) {
                ret = i;
                break;
            }
        }
    }
    if(ret == -1) {
        for(ret = 0; ret < 256; ret++)
            if(!cmap[ret].isValid())
                break;
        if(ret == 256) {
            ret = -1;
            qWarning("QGLContext::colorIndex(): Internal error!");
        } else {
            cmap[ret] = c;

            GLint vals[4];
            vals[0] = ret;
            vals[1] = c.red();
            vals[2] = c.green();
            vals[3] = c.blue();
            aglSetInteger((AGLContext)d_func()->cx, AGL_COLORMAP_ENTRY, vals);
        }
    }
    return (uint)(ret == -1 ? 0 : ret);
#else
    Q_UNUSED(c);
    return 0;
#endif
}

void QGLContext::generateFontDisplayLists(const QFont & /* fnt */, int /* listBase */)
{
}

static CFBundleRef qt_getOpenGLBundle()
{
    CFBundleRef bundle = 0;
    CFStringRef urlString = QCFString::toCFStringRef(QLatin1String("/System/Library/Frameworks/OpenGL.framework"));
    QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
                 urlString, kCFURLPOSIXPathStyle, false);
    if (url)
        bundle = CFBundleCreate(kCFAllocatorDefault, url);
    CFRelease(urlString);
    return bundle;
}

void *QGLContext::getProcAddress(const QString &proc) const
{
    CFStringRef procName = QCFString(proc).toCFStringRef(proc);
    void *result = CFBundleGetFunctionPointerForName(QCFType<CFBundleRef>(qt_getOpenGLBundle()),
                                             procName);
    CFRelease(procName);
    return result;
}
#ifndef QT_MAC_USE_COCOA
/*****************************************************************************
  QGLWidget AGL-specific code
 *****************************************************************************/

/****************************************************************************
  Hacks to glue AGL to an HIView
 ***************************************************************************/
QRegion qt_mac_get_widget_rgn(const QWidget *widget)
{
    if(!widget->isVisible() || widget->isMinimized())
        return QRegion();
    const QRect wrect = QRect(qt_mac_posInWindow(widget), widget->size());
    if(!wrect.isValid())
        return QRegion();

    RgnHandle macr = qt_mac_get_rgn();
    GetControlRegion((HIViewRef)widget->winId(), kControlStructureMetaPart, macr);
    OffsetRgn(macr, wrect.x(), wrect.y());
    QRegion ret = qt_mac_convert_mac_region(macr);

    QPoint clip_pos = wrect.topLeft();
    for(const QWidget *last_clip = 0, *clip = widget; clip; last_clip = clip, clip = clip->parentWidget()) {
        if(clip != widget) {
            GetControlRegion((HIViewRef)clip->winId(), kControlStructureMetaPart, macr);
            OffsetRgn(macr, clip_pos.x(), clip_pos.y());
            ret &= qt_mac_convert_mac_region(macr);
        }
        const QObjectList &children = clip->children();
        for(int i = children.size()-1; i >= 0; --i) {
            if(QWidget *child = qobject_cast<QWidget*>(children.at(i))) {
                if(child == last_clip)
                    break;

                // This check may seem weird, but when we are using a unified toolbar
                // The widget is actually being owned by that toolbar and not by Qt.
                // This means that the geometry it reports will be wrong
                // and will accidentally cause problems when calculating the region
                // So, it is better to skip these widgets since they aren't the hierarchy
                // anyway.
                if (HIViewGetSuperview(HIViewRef(child->winId())) != HIViewRef(clip->winId()))
                    continue;

                if(child->isVisible() && !child->isMinimized() && !child->isTopLevel()) {
                    const QRect childRect = QRect(clip_pos+child->pos(), child->size());
                    if(childRect.isValid() && wrect.intersects(childRect)) {
                        GetControlRegion((HIViewRef)child->winId(), kControlStructureMetaPart, macr);
                        OffsetRgn(macr, childRect.x(), childRect.y());
                        ret -= qt_mac_convert_mac_region(macr);
                    }
                }
            }
        }
        if(clip->isWindow())
            break;
        clip_pos -= clip->pos();
    }
    qt_mac_dispose_rgn(macr);
    return ret;
}

#endif

void QGLWidget::setMouseTracking(bool enable)
{
    QWidget::setMouseTracking(enable);
}

void QGLWidget::resizeEvent(QResizeEvent *)
{
    Q_D(QGLWidget);
    if (!isValid())
        return;
#ifndef QT_MAC_USE_COCOA
    if (!isWindow())
        d->glcx->d_func()->update = true;
#endif
    makeCurrent();
    if (!d->glcx->initialized())
        glInit();
#ifdef QT_MAC_USE_COCOA
    d->glcx->updatePaintDevice();
#endif
#ifndef QT_MAC_USE_COCOA
    float scale  = qt_mac_get_scale_factor(this);
    resizeGL(width() * scale, height() * scale);
#else
    resizeGL(width(), height());
#endif
}

const QGLContext* QGLWidget::overlayContext() const
{
    return 0;
}

void QGLWidget::makeOverlayCurrent()
{
}

void QGLWidget::updateOverlayGL()
{
}

void QGLWidget::setContext(QGLContext *context, const QGLContext* shareContext, bool deleteOldContext)
{
    Q_D(QGLWidget);
    if (context == 0) {
        qWarning("QGLWidget::setContext: Cannot set null context");
        return;
    }

    if (d->glcx)
        d->glcx->doneCurrent();
    QGLContext* oldcx = d->glcx;
    d->glcx = context;
    if (!d->glcx->isValid())
        d->glcx->create(shareContext ? shareContext : oldcx);
    if (deleteOldContext && oldcx)
        delete oldcx;
}

void QGLWidgetPrivate::init(QGLContext *context, const QGLWidget *shareWidget)
{
    Q_Q(QGLWidget);

    initContext(context, shareWidget);

    QWidget *current = q;
    while (current) {
        qt_widget_private(current)->glWidgets.append(QWidgetPrivate::GlWidgetInfo(q));
        if (current->isWindow())
            break;
        current = current->parentWidget();
    }
}

bool QGLWidgetPrivate::renderCxPm(QPixmap*)
{
    return false;
}

void QGLWidgetPrivate::cleanupColormaps()
{
}

const QGLColormap & QGLWidget::colormap() const
{
    return d_func()->cmap;
}

void QGLWidget::setColormap(const QGLColormap &)
{
}

void QGLWidgetPrivate::updatePaintDevice()
{
    Q_Q(QGLWidget);
    glcx->updatePaintDevice();
    q->update();
}

#endif

QT_END_NAMESPACE