aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/gl/GrGLCreateDebugInterface.cpp
blob: 5b8c315414fad5910835937cc92d1d563b6f3ef5 (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
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736

/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#include "gl/GrGLInterface.h"

#include "SkTArray.h"
#include "SkTDArray.h"

// the OpenGLES 2.0 spec says this must be >= 128
static const GrGLint kDefaultMaxVertexUniformVectors = 128;

// the OpenGLES 2.0 spec says this must be >=16
static const GrGLint kDefaultMaxFragmentUniformVectors = 16;

// the OpenGLES 2.0 spec says this must be >= 8
static const GrGLint kDefaultMaxVertexAttribs = 8;

// the OpenGLES 2.0 spec says this must be >= 8
static const GrGLint kDefaultMaxVaryingVectors = 8;

////////////////////////////////////////////////////////////////////////////////
// Each class derived from GrFakeRefObj should use this macro to add a
// factory creation entry point. This entry point is used by the GrGLDebug
// object to instantiate the various objects
// all globally unique IDs
#define GR_DEFINE_CREATOR(className)                        \
    public:                                                 \
    static GrFakeRefObj *create ## className() {            \
        return SkNEW(className);                            \
    }

////////////////////////////////////////////////////////////////////////////////
// Helper macro to make creating an object (where you need to get back a derived
// type) easier
#define GR_CREATE(className, classEnum)                     \
    reinterpret_cast<className *>(GrDebugGL::getInstance()->createObj(classEnum))

////////////////////////////////////////////////////////////////////////////////
// Helper macro to make finding objects less painful
#define GR_FIND(id, className, classEnum)                   \
    reinterpret_cast<className *>(GrDebugGL::getInstance()->findObject(id, classEnum))



////////////////////////////////////////////////////////////////////////////////
// This object is used to track the OpenGL objects. We don't use real
// reference counting (i.e., we don't free the objects when their ref count 
// goes to 0) so that we can detect invalid memory accesses. The refs we
// are tracking in this class are actually OpenGL's references to the objects
// not "ours"
// Each object also gets a unique globally identifying ID
class GrFakeRefObj {
public:
    GrFakeRefObj() 
        : fRef(0)
        , fHighRefCount(0)
        , fMarkedForDeletion(false)
        , fDeleted(false) {

        static int fNextID = 0;  // source for globally unique IDs - 0 is reserved!

        fID = ++fNextID;
    }
    virtual ~GrFakeRefObj() {};

    void ref() { 
        fRef++; 
        if (fHighRefCount < fRef) {
            fHighRefCount = fRef;
        }
    }
    void unref() { 
        fRef--; 
        GrAlwaysAssert(fRef >= 0);

        // often in OpenGL a given object may still be in use when the 
        // delete call is made. In these cases the object is marked
        // for deletion and then freed when it is no longer in use
        if (0 == fRef && fMarkedForDeletion) {
            this->deleteAction(); 
        }
    }
    int getRefCount() const { return fRef; }
    int getHighRefCount() const { return fHighRefCount; }

    GrGLuint getID() const { return fID; }

    void setMarkedForDeletion() { fMarkedForDeletion = true; }
    bool getMarkedForDeletion() const { return fMarkedForDeletion; }

    bool getDeleted() const     { return fDeleted; }

    // The deleteAction fires if the object has been marked for deletion but
    // couldn't be deleted earlier due to refs
    virtual void deleteAction() {
        this->setDeleted();
    }

protected:
private:
    int         fRef;
    int         fHighRefCount;      // high water mark of the ref count
    GrGLuint    fID;
    bool        fMarkedForDeletion;
    // The deleted flag is only set when OpenGL thinks the object is deleted
    // It is obviously still allocated w/in this framework
    bool        fDeleted;

    // setDeleted should only ever appear in the deleteAction method!
    void setDeleted()           { fDeleted = true; }
};

////////////////////////////////////////////////////////////////////////////////
class GrBufferObj : public GrFakeRefObj {
    GR_DEFINE_CREATOR(GrBufferObj);

public:
    GrBufferObj() 
        : GrFakeRefObj()
        , fDataPtr(NULL)
        , fMapped(false)
        , fBound(false)
        , fSize(0)
        , fUsage(GR_GL_STATIC_DRAW) {
    }
    virtual ~GrBufferObj() {
        delete[] fDataPtr;
    }

    void access() {
        // cannot access the buffer if it is currently mapped
        GrAlwaysAssert(!fMapped);
    }

    void setMapped()        { fMapped = true; }
    void resetMapped()      { fMapped = false; }
    bool getMapped() const  { return fMapped; }

    void setBound()         { fBound = true; }
    void resetBound()       { fBound = false; }
    bool getBound() const   { return fBound; }

    void allocate(GrGLint size, const GrGLchar *dataPtr) {
        GrAlwaysAssert(size >= 0);

        // delete pre-existing data
        delete[] fDataPtr;

        fSize = size;
        fDataPtr = new GrGLchar[size];
        if (dataPtr) {
            memcpy(fDataPtr, dataPtr, fSize);
        }
        // TODO: w/ no dataPtr the data is unitialized - this could be tracked
    }
    GrGLint getSize() const { return fSize; }
    GrGLchar *getDataPtr()  { return fDataPtr; }

    GrGLint getUsage() const { return fUsage; }
    void setUsage(GrGLint usage) { fUsage = usage; }

    virtual void deleteAction() SK_OVERRIDE {

        // buffers are automatically unmapped when deleted
        this->resetMapped();

        this->INHERITED::deleteAction();
    }


protected:
private:

    GrGLchar*   fDataPtr;
    bool        fMapped;       // is the buffer object mapped via "glMapBuffer"?
    bool        fBound;        // is the buffer object bound via "glBindBuffer"?
    GrGLint     fSize;         // size in bytes
    GrGLint     fUsage;        // one of: GL_STREAM_DRAW, GL_STATIC_DRAW, GL_DYNAMIC_DRAW

    typedef GrFakeRefObj INHERITED;
};

////////////////////////////////////////////////////////////////////////////////
// A common base class for render buffers and textures
class GrFBBindable : public GrFakeRefObj {

public:
    GrFBBindable() 
        : GrFakeRefObj() {
    }

    virtual ~GrFBBindable() {
        GrAlwaysAssert(0 == fColorReferees.count());
        GrAlwaysAssert(0 == fDepthReferees.count());
        GrAlwaysAssert(0 == fStencilReferees.count());
    }

    void setColorBound(GrFakeRefObj *referee) { 
        fColorReferees.append(1, &referee);
    }
    void resetColorBound(GrFakeRefObj *referee) { 
        int index = fColorReferees.find(referee);
        GrAlwaysAssert(0 <= index);
        fColorReferees.removeShuffle(index);
    }
    bool getColorBound(GrFakeRefObj *referee) const { 
        int index = fColorReferees.find(referee);
        return 0 <= index;
    }
    bool getColorBound() const { 
        return 0 != fColorReferees.count();
    }

    void setDepthBound(GrFakeRefObj *referee) { 
        fDepthReferees.append(1, &referee);
    }
    void resetDepthBound(GrFakeRefObj *referee) { 
        int index = fDepthReferees.find(referee);
        GrAlwaysAssert(0 <= index);
        fDepthReferees.removeShuffle(index);
    }
    bool getDepthBound(GrFakeRefObj *referee) const { 
        int index = fDepthReferees.find(referee);
        return 0 <= index;
    }
    bool getDepthBound() const { 
        return 0 != fDepthReferees.count();
    }

    void setStencilBound(GrFakeRefObj *referee) { 
        fStencilReferees.append(1, &referee);
    }
    void resetStencilBound(GrFakeRefObj *referee) { 
        int index = fStencilReferees.find(referee);
        GrAlwaysAssert(0 <= index);
        fStencilReferees.removeShuffle(index);
    }
    bool getStencilBound(GrFakeRefObj *referee) const { 
        int index = fStencilReferees.find(referee);
        return 0 <= index;
    }
    bool getStencilBound() const { 
        return 0 != fStencilReferees.count();
    }


protected:
private:
    SkTDArray<GrFakeRefObj *> fColorReferees;   // frame buffers that use this as a color buffer (via "glFramebufferRenderbuffer" or "glFramebufferTexture2D")
    SkTDArray<GrFakeRefObj *> fDepthReferees;   // frame buffers that use this as a depth buffer (via "glFramebufferRenderbuffer" or "glFramebufferTexture2D")
    SkTDArray<GrFakeRefObj *> fStencilReferees; // frame buffers that use this as a stencil buffer (via "glFramebufferRenderbuffer" or "glFramebufferTexture2D")

    typedef GrFakeRefObj INHERITED;
};

////////////////////////////////////////////////////////////////////////////////
class GrRenderBufferObj : public GrFBBindable {
    GR_DEFINE_CREATOR(GrRenderBufferObj);

public:
    GrRenderBufferObj()
        : GrFBBindable()
        , fBound(false) {
    }

    void setBound()         { fBound = true; }
    void resetBound()       { fBound = false; }
    bool getBound() const   { return fBound; }

    virtual void deleteAction() SK_OVERRIDE {

        this->INHERITED::deleteAction();
    }

protected:
private:
    bool fBound;           // is this render buffer currently bound via "glBindRenderbuffer"?

    typedef GrFBBindable INHERITED;
};

class GrTextureUnitObj;

////////////////////////////////////////////////////////////////////////////////
class GrTextureObj : public GrFBBindable {
    GR_DEFINE_CREATOR(GrTextureObj);

public:
    GrTextureObj() 
        : GrFBBindable() {
    }

    virtual ~GrTextureObj() {
        GrAlwaysAssert(0 == fTextureUnitReferees.count());
    }

    void setBound(GrTextureUnitObj *referee) { 
        fTextureUnitReferees.append(1, &referee);
    }

    void resetBound(GrTextureUnitObj *referee) { 
        int index = fTextureUnitReferees.find(referee);
        GrAlwaysAssert(0 <= index);
        fTextureUnitReferees.removeShuffle(index);
    }
    bool getBound(GrTextureUnitObj *referee) const { 
        int index = fTextureUnitReferees.find(referee);
        return 0 <= index;
    }
    bool getBound() const { 
        return 0 != fTextureUnitReferees.count();
    }

    virtual void deleteAction() SK_OVERRIDE {

        this->INHERITED::deleteAction();
    }

protected:

private:
    SkTDArray<GrTextureUnitObj *> fTextureUnitReferees;   // texture units that bind this texture (via "glBindTexture")

    typedef GrFBBindable INHERITED;
};

////////////////////////////////////////////////////////////////////////////////
// Although texture unit objects are allocated & deallocated like the other
// GL emulation objects they are derived from GrFakeRefObj to provide some
// uniformity in how the GrDebugGL class manages resources
class GrTextureUnitObj : public GrFakeRefObj {
    GR_DEFINE_CREATOR(GrTextureUnitObj);

public:
    GrTextureUnitObj()
        : GrFakeRefObj()
        , fNumber(0)
        , fTexture(NULL) {
    }

    void setNumber(GrGLenum number) {
        fNumber = number;
    }
    GrGLenum getNumber() const { return fNumber; }

    void setTexture(GrTextureObj *texture)  { 

        if (fTexture) {
            GrAlwaysAssert(fTexture->getBound(this));
            fTexture->resetBound(this);

            GrAlwaysAssert(!fTexture->getDeleted());
            fTexture->unref();
        }

        fTexture = texture; 

        if (fTexture) {
            GrAlwaysAssert(!fTexture->getDeleted());
            fTexture->ref();

            GrAlwaysAssert(!fTexture->getBound(this));
            fTexture->setBound(this);
        }
    }

    GrTextureObj *getTexture()                  { return fTexture; }

protected:
private:
    GrGLenum fNumber;
    GrTextureObj *fTexture;

    typedef GrFakeRefObj INHERITED;
};


////////////////////////////////////////////////////////////////////////////////
// TODO: when a framebuffer obj is bound the GL_SAMPLES query must return 0
// TODO: GL_STENCIL_BITS must also be redirected to the framebuffer
class GrFrameBufferObj : public GrFakeRefObj {
    GR_DEFINE_CREATOR(GrFrameBufferObj);

public:
    GrFrameBufferObj()
        : GrFakeRefObj()
        , fBound(false)
        , fColorBuffer(NULL)
        , fDepthBuffer(NULL)
        , fStencilBuffer(NULL) {
    }

    virtual ~GrFrameBufferObj() {
        fColorBuffer = NULL;
        fDepthBuffer = NULL;
        fStencilBuffer = NULL;
    }

    void setBound()         { fBound = true; }
    void resetBound()       { fBound = false; }
    bool getBound() const   { return fBound; }

    void setColor(GrFBBindable *buffer) {
        if (fColorBuffer) {
            // automatically break the binding of the old buffer
            GrAlwaysAssert(fColorBuffer->getColorBound(this));
            fColorBuffer->resetColorBound(this);

            GrAlwaysAssert(!fColorBuffer->getDeleted());
            fColorBuffer->unref();
        }
        fColorBuffer = buffer;
        if (fColorBuffer) {
            GrAlwaysAssert(!fColorBuffer->getDeleted());
            fColorBuffer->ref();

            GrAlwaysAssert(!fColorBuffer->getColorBound(this));
            fColorBuffer->setColorBound(this);
        }
    }
    GrFBBindable *getColor()       { return fColorBuffer; }

    void setDepth(GrFBBindable *buffer) {
        if (fDepthBuffer) {
            // automatically break the binding of the old buffer
            GrAlwaysAssert(fDepthBuffer->getDepthBound(this));
            fDepthBuffer->resetDepthBound(this);

            GrAlwaysAssert(!fDepthBuffer->getDeleted());
            fDepthBuffer->unref();
        }
        fDepthBuffer = buffer;
        if (fDepthBuffer) {
            GrAlwaysAssert(!fDepthBuffer->getDeleted());
            fDepthBuffer->ref();

            GrAlwaysAssert(!fDepthBuffer->getDepthBound(this));
            fDepthBuffer->setDepthBound(this);
        }
    }
    GrFBBindable *getDepth()       { return fDepthBuffer; }

    void setStencil(GrFBBindable *buffer) {
        if (fStencilBuffer) {
            // automatically break the binding of the old buffer
            GrAlwaysAssert(fStencilBuffer->getStencilBound(this));
            fStencilBuffer->resetStencilBound(this);

            GrAlwaysAssert(!fStencilBuffer->getDeleted());
            fStencilBuffer->unref();
        }
        fStencilBuffer = buffer;
        if (fStencilBuffer) {
            GrAlwaysAssert(!fStencilBuffer->getDeleted());
            fStencilBuffer->ref();

            GrAlwaysAssert(!fStencilBuffer->getStencilBound(this));
            fStencilBuffer->setStencilBound(this);
        }
    }
    GrFBBindable *getStencil()     { return fStencilBuffer; }

    virtual void deleteAction() SK_OVERRIDE {

        setColor(NULL);
        setDepth(NULL);
        setStencil(NULL);

        this->INHERITED::deleteAction();
    }

protected:
private:
    bool fBound;        // is this frame buffer currently bound via "glBindFramebuffer"?
    GrFBBindable * fColorBuffer;
    GrFBBindable * fDepthBuffer;
    GrFBBindable * fStencilBuffer;

    typedef GrFakeRefObj INHERITED;
};

////////////////////////////////////////////////////////////////////////////////
class GrShaderObj : public GrFakeRefObj {
    GR_DEFINE_CREATOR(GrShaderObj);

public:
    GrShaderObj() 
        : GrFakeRefObj()
        , fType(GR_GL_VERTEX_SHADER)   {}

    void setType(GrGLenum type) { fType = type; }
    GrGLenum getType()  { return fType; }

    virtual void deleteAction() SK_OVERRIDE {

        this->INHERITED::deleteAction();
    }

protected:
private:
    GrGLenum fType;  // either GR_GL_VERTEX_SHADER or GR_GL_FRAGMENT_SHADER

    typedef GrFakeRefObj INHERITED;
};


////////////////////////////////////////////////////////////////////////////////
class GrProgramObj : public GrFakeRefObj {
    GR_DEFINE_CREATOR(GrProgramObj);

public:
    GrProgramObj() 
        : GrFakeRefObj()
        , fInUse(false) {}

    void AttachShader(GrShaderObj *shader) {
        shader->ref();
        fShaders.push_back(shader);
    }

    virtual void deleteAction() SK_OVERRIDE {

        // shaders are automatically detached from a deleted program. They will only be
        // deleted if they were marked for deletion by a prior call to glDeleteShader
        for (int i = 0; i < fShaders.count(); ++i) {
            fShaders[i]->unref();
        }
        fShaders.reset();

        this->INHERITED::deleteAction();
    }

    // TODO: this flag system won't work w/ multiple contexts!
    void setInUse()         { fInUse = true; }
    void resetInUse()       { fInUse = false; }
    bool getInUse() const   { return fInUse; }

protected:

private:
    SkTArray<GrShaderObj *> fShaders;
    bool fInUse;            // has this program been activated by a glUseProgram call?

    typedef GrFakeRefObj INHERITED;
};

////////////////////////////////////////////////////////////////////////////////
// This is the main debugging object. It is a singleton and keeps track of
// all the other debug objects.
class GrDebugGL {
public:
    enum GrObjTypes {
        kTexture_ObjTypes = 0,
        kBuffer_ObjTypes,
        kRenderBuffer_ObjTypes,
        kFrameBuffer_ObjTypes,
        kShader_ObjTypes,
        kProgram_ObjTypes,
        kTextureUnit_ObjTypes,
        kObjTypeCount
    };

    GrFakeRefObj *createObj(GrObjTypes type) {
        GrFakeRefObj *temp = (*gFactoryFunc[type])();

        fObjects.push_back(temp);

        return temp;
    }

    GrFakeRefObj *findObject(GrGLuint ID, GrObjTypes type) {
        for (int i = 0; i < fObjects.count(); ++i) {
            if (fObjects[i]->getID() == ID) { // && fObjects[i]->getType() == type) {
                // The application shouldn't be accessing objects
                // that (as far as OpenGL knows) were already deleted 
                GrAlwaysAssert(!fObjects[i]->getDeleted());
                GrAlwaysAssert(!fObjects[i]->getMarkedForDeletion());
                return fObjects[i];
            }
        }

        return NULL;
    }

    GrGLuint getMaxTextureUnits() const { return kDefaultMaxTextureUnits; }

    void setCurTextureUnit(GrGLuint curTextureUnit) { fCurTextureUnit = curTextureUnit; }
    GrGLuint getCurTextureUnit() const { return fCurTextureUnit; }

    GrTextureUnitObj *getTextureUnit(int iUnit) {
        GrAlwaysAssert(0 <= iUnit && kDefaultMaxTextureUnits > iUnit);

        return fTextureUnits[iUnit];
    }

    void setArrayBuffer(GrBufferObj *arrayBuffer) { 
        if (fArrayBuffer) {
            // automatically break the binding of the old buffer
            GrAlwaysAssert(fArrayBuffer->getBound());
            fArrayBuffer->resetBound();

            GrAlwaysAssert(!fArrayBuffer->getDeleted());
            fArrayBuffer->unref();
        }

        fArrayBuffer = arrayBuffer; 

        if (fArrayBuffer) {
            GrAlwaysAssert(!fArrayBuffer->getDeleted());
            fArrayBuffer->ref();

            GrAlwaysAssert(!fArrayBuffer->getBound());
            fArrayBuffer->setBound();
        }
    }
    GrBufferObj *getArrayBuffer()                   { return fArrayBuffer; }

    void setElementArrayBuffer(GrBufferObj *elementArrayBuffer) { 
        if (fElementArrayBuffer) {
            // automatically break the binding of the old buffer
            GrAlwaysAssert(fElementArrayBuffer->getBound());
            fElementArrayBuffer->resetBound();

            GrAlwaysAssert(!fElementArrayBuffer->getDeleted());
            fElementArrayBuffer->unref();
        }

        fElementArrayBuffer = elementArrayBuffer; 

        if (fElementArrayBuffer) {
            GrAlwaysAssert(!fElementArrayBuffer->getDeleted());
            fElementArrayBuffer->ref();

            GrAlwaysAssert(!fElementArrayBuffer->getBound());
            fElementArrayBuffer->setBound();
        }
    }

    GrBufferObj *getElementArrayBuffer()                            { return fElementArrayBuffer; }

    void setTexture(GrTextureObj *texture)  { 
        fTextureUnits[fCurTextureUnit]->setTexture(texture);
    }

    void setFrameBuffer(GrFrameBufferObj *frameBuffer)  { 
        if (fFrameBuffer) {
            GrAlwaysAssert(fFrameBuffer->getBound());
            fFrameBuffer->resetBound();

            GrAlwaysAssert(!fFrameBuffer->getDeleted());
            fFrameBuffer->unref();
        }

        fFrameBuffer = frameBuffer; 

        if (fFrameBuffer) {
            GrAlwaysAssert(!fFrameBuffer->getDeleted());
            fFrameBuffer->ref();

            GrAlwaysAssert(!fFrameBuffer->getBound());
            fFrameBuffer->setBound();
        }
    }

    GrFrameBufferObj *getFrameBuffer()                  { return fFrameBuffer; }

    void setRenderBuffer(GrRenderBufferObj *renderBuffer)  { 
        if (fRenderBuffer) {
            GrAlwaysAssert(fRenderBuffer->getBound());
            fRenderBuffer->resetBound();

            GrAlwaysAssert(!fRenderBuffer->getDeleted());
            fRenderBuffer->unref();
        }

        fRenderBuffer = renderBuffer; 

        if (fRenderBuffer) {
            GrAlwaysAssert(!fRenderBuffer->getDeleted());
            fRenderBuffer->ref();

            GrAlwaysAssert(!fRenderBuffer->getBound());
            fRenderBuffer->setBound();
        }
    }

    GrRenderBufferObj *getRenderBuffer()                  { return fRenderBuffer; }

    void useProgram(GrProgramObj *program) {
        if (fProgram) {
            GrAlwaysAssert(fProgram->getInUse());
            fProgram->resetInUse();

            GrAlwaysAssert(!fProgram->getDeleted());
            fProgram->unref();
        }

        fProgram = program;

        if (fProgram) {
            GrAlwaysAssert(!fProgram->getDeleted());
            fProgram->ref();

            GrAlwaysAssert(!fProgram->getInUse());
            fProgram->setInUse();
        }
    }

    static GrDebugGL *getInstance() {
//        static GrDebugGL Obj;

        return &Obj;
    }

    void report() const {
        for (int i = 0; i < fObjects.count(); ++i) {
            GrAlwaysAssert(0 == fObjects[i]->getRefCount());
            GrAlwaysAssert(0 < fObjects[i]->getHighRefCount());
            GrAlwaysAssert(fObjects[i]->getDeleted());
        }
    }

protected:

private:
    // the OpenGLES 2.0 spec says this must be >= 2
    static const GrGLint kDefaultMaxTextureUnits = 8;

    GrGLuint        fMaxTextureUnits;
    GrGLuint        fCurTextureUnit;
    GrBufferObj *   fArrayBuffer;
    GrBufferObj *   fElementArrayBuffer;
    GrFrameBufferObj *fFrameBuffer;
    GrRenderBufferObj *fRenderBuffer;
    GrProgramObj *  fProgram;
    GrTextureObj *  fTexture;
    GrTextureUnitObj *fTextureUnits[kDefaultMaxTextureUnits];

    typedef GrFakeRefObj *(*Create)();

    static Create gFactoryFunc[kObjTypeCount];

    static GrDebugGL Obj;

    // global store of all objects
    SkTArray<GrFakeRefObj *> fObjects;

    GrDebugGL() 
        : fCurTextureUnit(0)
        , fArrayBuffer(NULL)
        , fElementArrayBuffer(NULL)
        , fFrameBuffer(NULL)
        , fRenderBuffer(NULL)
        , fProgram(NULL)
        , fTexture(NULL) {

        for (int i = 0; i < kDefaultMaxTextureUnits; ++i) {
            fTextureUnits[i] = GR_CREATE(GrTextureUnitObj, GrDebugGL::kTextureUnit_ObjTypes);
            fTextureUnits[i]->ref();

            fTextureUnits[i]->setNumber(i);
        }
    }

    ~GrDebugGL() {
        // unref & delete the texture units first so they don't show up on the leak report
        for (int i = 0; i < kDefaultMaxTextureUnits; ++i) {
            fTextureUnits[i]->unref();
            fTextureUnits[i]->deleteAction();
        }

        this->report();

        for (int i = 0; i < fObjects.count(); ++i) {
            delete fObjects[i];
        }
        fObjects.reset();

        fArrayBuffer = NULL;
        fElementArrayBuffer = NULL;
        fFrameBuffer = NULL;
        fRenderBuffer = NULL;
        fProgram = NULL;
        fTexture = NULL;
    }
};

GrDebugGL GrDebugGL::Obj;
GrDebugGL::Create GrDebugGL::gFactoryFunc[kObjTypeCount] = {
    GrTextureObj::createGrTextureObj,
    GrBufferObj::createGrBufferObj,
    GrRenderBufferObj::createGrRenderBufferObj,
    GrFrameBufferObj::createGrFrameBufferObj,
    GrShaderObj::createGrShaderObj,
    GrProgramObj::createGrProgramObj,
    GrTextureUnitObj::createGrTextureUnitObj,
};


////////////////////////////////////////////////////////////////////////////////
GrGLvoid GR_GL_FUNCTION_TYPE debugGLActiveTexture(GrGLenum texture) {
    
    // Ganesh offsets the texture unit indices
    texture -= GR_GL_TEXTURE0;
    GrAlwaysAssert(texture < GrDebugGL::getInstance()->getMaxTextureUnits());

    GrDebugGL::getInstance()->setCurTextureUnit(texture);
}

////////////////////////////////////////////////////////////////////////////////
GrGLvoid GR_GL_FUNCTION_TYPE debugGLAttachShader(GrGLuint programID, GrGLuint shaderID) {

    GrProgramObj *program = GR_FIND(programID, GrProgramObj, GrDebugGL::kProgram_ObjTypes);
    GrAlwaysAssert(program);

    GrShaderObj *shader = GR_FIND(shaderID, GrShaderObj, GrDebugGL::kShader_ObjTypes);
    GrAlwaysAssert(shader);

    program->AttachShader(shader);
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLBeginQuery(GrGLenum target, GrGLuint id) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLBindAttribLocation(GrGLuint program, GrGLuint index, const char* name) {}

////////////////////////////////////////////////////////////////////////////////
GrGLvoid GR_GL_FUNCTION_TYPE debugGLBindTexture(GrGLenum target, GrGLuint textureID) {

    // we don't use cube maps
    GrAlwaysAssert(target == GR_GL_TEXTURE_2D); // || target == GR_GL_TEXTURE_CUBE_MAP);

    // a textureID of 0 is acceptable - it binds to the default texture target
    GrTextureObj *texture = GR_FIND(textureID, GrTextureObj, GrDebugGL::kTexture_ObjTypes);

    GrDebugGL::getInstance()->setTexture(texture);
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLBlendColor(GrGLclampf red, GrGLclampf green, GrGLclampf blue, GrGLclampf alpha) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLBindFragDataLocation(GrGLuint program, GrGLuint colorNumber, const GrGLchar* name) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLBlendFunc(GrGLenum sfactor, GrGLenum dfactor) {}

////////////////////////////////////////////////////////////////////////////////
GrGLvoid GR_GL_FUNCTION_TYPE debugGLBufferData(GrGLenum target, GrGLsizeiptr size, const GrGLvoid* data, GrGLenum usage) {
    GrAlwaysAssert(GR_GL_ARRAY_BUFFER == target || GR_GL_ELEMENT_ARRAY_BUFFER == target);
    GrAlwaysAssert(size >= 0);
    GrAlwaysAssert(GR_GL_STREAM_DRAW == usage || GR_GL_STATIC_DRAW == usage || GR_GL_DYNAMIC_DRAW == usage);

    GrBufferObj *buffer = NULL;
    switch (target) {
        case GR_GL_ARRAY_BUFFER:
            buffer = GrDebugGL::getInstance()->getArrayBuffer();
            break;
        case GR_GL_ELEMENT_ARRAY_BUFFER:
            buffer = GrDebugGL::getInstance()->getElementArrayBuffer();
            break;
        default:
            GrCrash("Unexpected target to glBufferData");
            break;
    }

    GrAlwaysAssert(buffer);
    GrAlwaysAssert(buffer->getBound());

    buffer->allocate(size, reinterpret_cast<const GrGLchar *>(data));
    buffer->setUsage(usage);
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLBufferSubData(GrGLenum target, GrGLintptr offset, GrGLsizeiptr size, const GrGLvoid* data) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLClear(GrGLbitfield mask) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLClearColor(GrGLclampf red, GrGLclampf green, GrGLclampf blue, GrGLclampf alpha) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLClearStencil(GrGLint s) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLColorMask(GrGLboolean red, GrGLboolean green, GrGLboolean blue, GrGLboolean alpha) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLCompileShader(GrGLuint shader) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLCompressedTexImage2D(GrGLenum target, GrGLint level, GrGLenum internalformat, GrGLsizei width, GrGLsizei height, GrGLint border, GrGLsizei imageSize, const GrGLvoid* data) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLCullFace(GrGLenum mode) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLDepthMask(GrGLboolean flag) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLDisable(GrGLenum cap) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLDisableVertexAttribArray(GrGLuint index) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLDrawArrays(GrGLenum mode, GrGLint first, GrGLsizei count) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLDrawBuffer(GrGLenum mode) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLDrawBuffers(GrGLsizei n, const GrGLenum* bufs) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLDrawElements(GrGLenum mode, GrGLsizei count, GrGLenum type, const GrGLvoid* indices) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLEnable(GrGLenum cap) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLEnableVertexAttribArray(GrGLuint index) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLEndQuery(GrGLenum target) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLFinish() {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLFlush() {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLFrontFace(GrGLenum mode) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLLineWidth(GrGLfloat width) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLLinkProgram(GrGLuint program) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLPixelStorei(GrGLenum pname, GrGLint param) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLQueryCounter(GrGLuint id, GrGLenum target) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLReadBuffer(GrGLenum src) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLReadPixels(GrGLint x, GrGLint y, GrGLsizei width, GrGLsizei height, GrGLenum format, GrGLenum type, GrGLvoid* pixels) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLScissor(GrGLint x, GrGLint y, GrGLsizei width, GrGLsizei height) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLShaderSource(GrGLuint shader, GrGLsizei count, const char** str, const GrGLint* length) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLStencilFunc(GrGLenum func, GrGLint ref, GrGLuint mask) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLStencilFuncSeparate(GrGLenum face, GrGLenum func, GrGLint ref, GrGLuint mask) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLStencilMask(GrGLuint mask) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLStencilMaskSeparate(GrGLenum face, GrGLuint mask) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLStencilOp(GrGLenum fail, GrGLenum zfail, GrGLenum zpass) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLStencilOpSeparate(GrGLenum face, GrGLenum fail, GrGLenum zfail, GrGLenum zpass) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLTexImage2D(GrGLenum target, GrGLint level, GrGLint internalformat, GrGLsizei width, GrGLsizei height, GrGLint border, GrGLenum format, GrGLenum type, const GrGLvoid* pixels) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLTexParameteri(GrGLenum target, GrGLenum pname, GrGLint param) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLTexStorage2D(GrGLenum target, GrGLsizei levels, GrGLenum internalformat, GrGLsizei width, GrGLsizei height) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLTexSubImage2D(GrGLenum target, GrGLint level, GrGLint xoffset, GrGLint yoffset, GrGLsizei width, GrGLsizei height, GrGLenum format, GrGLenum type, const GrGLvoid* pixels) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform1f(GrGLint location, GrGLfloat v0) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform1i(GrGLint location, GrGLint v0) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform1fv(GrGLint location, GrGLsizei count, const GrGLfloat* v) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform1iv(GrGLint location, GrGLsizei count, const GrGLint* v) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform2f(GrGLint location, GrGLfloat v0, GrGLfloat v1) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform2i(GrGLint location, GrGLint v0, GrGLint v1) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform2fv(GrGLint location, GrGLsizei count, const GrGLfloat* v) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform2iv(GrGLint location, GrGLsizei count, const GrGLint* v) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform3f(GrGLint location, GrGLfloat v0, GrGLfloat v1, GrGLfloat v2) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform3i(GrGLint location, GrGLint v0, GrGLint v1, GrGLint v2) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform3fv(GrGLint location, GrGLsizei count, const GrGLfloat* v) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform3iv(GrGLint location, GrGLsizei count, const GrGLint* v) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform4f(GrGLint location, GrGLfloat v0, GrGLfloat v1, GrGLfloat v2, GrGLfloat v3) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform4i(GrGLint location, GrGLint v0, GrGLint v1, GrGLint v2, GrGLint v3) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform4fv(GrGLint location, GrGLsizei count, const GrGLfloat* v) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniform4iv(GrGLint location, GrGLsizei count, const GrGLint* v) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniformMatrix2fv(GrGLint location, GrGLsizei count, GrGLboolean transpose, const GrGLfloat* value) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniformMatrix3fv(GrGLint location, GrGLsizei count, GrGLboolean transpose, const GrGLfloat* value) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLUniformMatrix4fv(GrGLint location, GrGLsizei count, GrGLboolean transpose, const GrGLfloat* value) {}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLUseProgram(GrGLuint programID) {

    // A programID of 0 is legal
    GrProgramObj *program = GR_FIND(programID, GrProgramObj, GrDebugGL::kProgram_ObjTypes);

    GrDebugGL::getInstance()->useProgram(program);
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLVertexAttrib4fv(GrGLuint indx, const GrGLfloat* values) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLVertexAttribPointer(GrGLuint indx, GrGLint size, GrGLenum type, GrGLboolean normalized, GrGLsizei stride, const GrGLvoid* ptr) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLViewport(GrGLint x, GrGLint y, GrGLsizei width, GrGLsizei height) {}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLBindFramebuffer(GrGLenum target, GrGLuint frameBufferID) {

    GrAlwaysAssert(GR_GL_FRAMEBUFFER == target);

    // a frameBufferID of 0 is acceptable - it binds to the default frame buffer
    GrFrameBufferObj *frameBuffer = GR_FIND(frameBufferID, GrFrameBufferObj, GrDebugGL::kFrameBuffer_ObjTypes);

    GrDebugGL::getInstance()->setFrameBuffer(frameBuffer);
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLBindRenderbuffer(GrGLenum target, GrGLuint renderBufferID) {

    GrAlwaysAssert(GR_GL_RENDERBUFFER == target);

    // a renderBufferID of 0 is acceptable - it unbinds the bound render buffer
    GrRenderBufferObj *renderBuffer = GR_FIND(renderBufferID, GrRenderBufferObj, GrDebugGL::kRenderBuffer_ObjTypes);

    GrDebugGL::getInstance()->setRenderBuffer(renderBuffer);
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLDeleteTextures(GrGLsizei n, const GrGLuint* textures) {

    // first potentially unbind the texture
    // TODO: move this into GrDebugGL as unBindTexture?
    for (unsigned int i = 0; i < GrDebugGL::getInstance()->getMaxTextureUnits(); ++i)
    {
        GrTextureUnitObj *pTU = GrDebugGL::getInstance()->getTextureUnit(i);

        if (pTU->getTexture()) {
            for (int j = 0; j < n; ++j) {

                if (textures[j] == pTU->getTexture()->getID()) {
                    // this ID is the current texture - revert the binding to 0
                    pTU->setTexture(NULL);
                }
            }
        }
    }

    // TODO: fuse the following block with DeleteRenderBuffers?
    // Open GL will remove a deleted render buffer from the active frame buffer but not
    // from any other frame buffer
    if (GrDebugGL::getInstance()->getFrameBuffer()) {

        GrFrameBufferObj *frameBuffer = GrDebugGL::getInstance()->getFrameBuffer();

        for (int i = 0; i < n; ++i) {

            if (NULL != frameBuffer->getColor() && textures[i] == frameBuffer->getColor()->getID()) {
                frameBuffer->setColor(NULL);
            } 
            if (NULL != frameBuffer->getDepth() && textures[i] == frameBuffer->getDepth()->getID()) {
                frameBuffer->setDepth(NULL);
            }
            if (NULL != frameBuffer->getStencil() && textures[i] == frameBuffer->getStencil()->getID()) {
                frameBuffer->setStencil(NULL);
            }
        }
    }

    // then actually "delete" the buffers
    for (int i = 0; i < n; ++i) {
        GrTextureObj *buffer = GR_FIND(textures[i], GrTextureObj, GrDebugGL::kTexture_ObjTypes);
        GrAlwaysAssert(buffer);

        // OpenGL gives no guarantees if a texture is deleted while attached to
        // something other than the currently bound frame buffer
        GrAlwaysAssert(!buffer->getBound());

        GrAlwaysAssert(!buffer->getDeleted());
        buffer->deleteAction();
    }

}


GrGLvoid GR_GL_FUNCTION_TYPE debugGLDeleteFramebuffers(GrGLsizei n, const GrGLuint *frameBuffers) {

    // first potentially unbind the buffers
    if (GrDebugGL::getInstance()->getFrameBuffer()) {
        for (int i = 0; i < n; ++i) {

            if (frameBuffers[i] == GrDebugGL::getInstance()->getFrameBuffer()->getID()) {
                // this ID is the current frame buffer - rebind to the default
                GrDebugGL::getInstance()->setFrameBuffer(NULL);
            }
        }
    }

    // then actually "delete" the buffers
    for (int i = 0; i < n; ++i) {
        GrFrameBufferObj *buffer = GR_FIND(frameBuffers[i], GrFrameBufferObj, GrDebugGL::kFrameBuffer_ObjTypes);
        GrAlwaysAssert(buffer);

        GrAlwaysAssert(!buffer->getDeleted());
        buffer->deleteAction();
    }
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLDeleteRenderbuffers(GrGLsizei n, const GrGLuint *renderBuffers) {

    // first potentially unbind the buffers
    if (GrDebugGL::getInstance()->getRenderBuffer()) {
        for (int i = 0; i < n; ++i) {

            if (renderBuffers[i] == GrDebugGL::getInstance()->getRenderBuffer()->getID()) {
                // this ID is the current render buffer - make no render buffer be bound
                GrDebugGL::getInstance()->setRenderBuffer(NULL);
            }
        }
    }

    // TODO: fuse the following block with DeleteTextures?
    // Open GL will remove a deleted render buffer from the active frame buffer but not
    // from any other frame buffer
    if (GrDebugGL::getInstance()->getFrameBuffer()) {

        GrFrameBufferObj *frameBuffer = GrDebugGL::getInstance()->getFrameBuffer();

        for (int i = 0; i < n; ++i) {

            if (NULL != frameBuffer->getColor() && renderBuffers[i] == frameBuffer->getColor()->getID()) {
                frameBuffer->setColor(NULL);
            } 
            if (NULL != frameBuffer->getDepth() && renderBuffers[i] == frameBuffer->getDepth()->getID()) {
                frameBuffer->setDepth(NULL);
            }
            if (NULL != frameBuffer->getStencil() && renderBuffers[i] == frameBuffer->getStencil()->getID()) {
                frameBuffer->setStencil(NULL);
            }
        }
    }

    // then actually "delete" the buffers
    for (int i = 0; i < n; ++i) {
        GrRenderBufferObj *buffer = GR_FIND(renderBuffers[i], GrRenderBufferObj, GrDebugGL::kRenderBuffer_ObjTypes);
        GrAlwaysAssert(buffer);

        // OpenGL gives no guarantees if a render buffer is deleted while attached to
        // something other than the currently bound frame buffer
        GrAlwaysAssert(!buffer->getColorBound());
        GrAlwaysAssert(!buffer->getDepthBound());
        GrAlwaysAssert(!buffer->getStencilBound());

        GrAlwaysAssert(!buffer->getDeleted());
        buffer->deleteAction();
    }
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLFramebufferRenderbuffer(GrGLenum target, GrGLenum attachment, GrGLenum renderbuffertarget, GrGLuint renderBufferID) {

    GrAlwaysAssert(GR_GL_FRAMEBUFFER == target);
    GrAlwaysAssert(GR_GL_COLOR_ATTACHMENT0 == attachment || 
                   GR_GL_DEPTH_ATTACHMENT == attachment || 
                   GR_GL_STENCIL_ATTACHMENT == attachment);
    GrAlwaysAssert(GR_GL_RENDERBUFFER == renderbuffertarget);

    GrFrameBufferObj *framebuffer = GrDebugGL::getInstance()->getFrameBuffer();
    // A render buffer cannot be attached to the default framebuffer
    GrAlwaysAssert(NULL != framebuffer);

    // a renderBufferID of 0 is acceptable - it unbinds the current render buffer
    GrRenderBufferObj *renderbuffer = GR_FIND(renderBufferID, GrRenderBufferObj, GrDebugGL::kRenderBuffer_ObjTypes);

    switch (attachment) {
    case GR_GL_COLOR_ATTACHMENT0:
        framebuffer->setColor(renderbuffer);
        break;
    case GR_GL_DEPTH_ATTACHMENT:
        framebuffer->setDepth(renderbuffer);
        break;
    case GR_GL_STENCIL_ATTACHMENT:
        framebuffer->setStencil(renderbuffer);
        break;
    default:
        GrAlwaysAssert(false);
        break;
    };

}

////////////////////////////////////////////////////////////////////////////////
GrGLvoid GR_GL_FUNCTION_TYPE debugGLFramebufferTexture2D(GrGLenum target, GrGLenum attachment, GrGLenum textarget, GrGLuint textureID, GrGLint level) {

    GrAlwaysAssert(GR_GL_FRAMEBUFFER == target);
    GrAlwaysAssert(GR_GL_COLOR_ATTACHMENT0 == attachment || 
                   GR_GL_DEPTH_ATTACHMENT == attachment || 
                   GR_GL_STENCIL_ATTACHMENT == attachment);
    GrAlwaysAssert(GR_GL_TEXTURE_2D == textarget);

    GrFrameBufferObj *framebuffer = GrDebugGL::getInstance()->getFrameBuffer();
    // A texture cannot be attached to the default framebuffer
    GrAlwaysAssert(NULL != framebuffer);

    // A textureID of 0 is allowed - it unbinds the currently bound texture
    GrTextureObj *texture = GR_FIND(textureID, GrTextureObj, GrDebugGL::kTexture_ObjTypes);
    if (texture) {
        // The texture shouldn't be bound to a texture unit - this could lead to a feedback loop
        GrAlwaysAssert(!texture->getBound());
    }

    GrAlwaysAssert(0 == level);

    switch (attachment) {
    case GR_GL_COLOR_ATTACHMENT0:
        framebuffer->setColor(texture);
        break;
    case GR_GL_DEPTH_ATTACHMENT:
        framebuffer->setDepth(texture);
        break;
    case GR_GL_STENCIL_ATTACHMENT:
        framebuffer->setStencil(texture);
        break;
    default:
        GrAlwaysAssert(false);
        break;
    };
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLGetFramebufferAttachmentParameteriv(GrGLenum target, GrGLenum attachment, GrGLenum pname, GrGLint* params) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLGetRenderbufferParameteriv(GrGLenum target, GrGLenum pname, GrGLint* params) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLRenderbufferStorage(GrGLenum target, GrGLenum internalformat, GrGLsizei width, GrGLsizei height) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLRenderbufferStorageMultisample(GrGLenum target, GrGLsizei samples, GrGLenum internalformat, GrGLsizei width, GrGLsizei height) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLBlitFramebuffer(GrGLint srcX0, GrGLint srcY0, GrGLint srcX1, GrGLint srcY1, GrGLint dstX0, GrGLint dstY0, GrGLint dstX1, GrGLint dstY1, GrGLbitfield mask, GrGLenum filter) {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLResolveMultisampleFramebuffer() {}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLBindFragDataLocationIndexed(GrGLuint program, GrGLuint colorNumber, GrGLuint index, const GrGLchar * name) {}

GrGLenum GR_GL_FUNCTION_TYPE debugGLCheckFramebufferStatus(GrGLenum target) {

    GrAlwaysAssert(GR_GL_FRAMEBUFFER == target);

    return GR_GL_FRAMEBUFFER_COMPLETE;
}

GrGLuint GR_GL_FUNCTION_TYPE debugGLCreateProgram() {

    GrProgramObj *program = GR_CREATE(GrProgramObj, GrDebugGL::kProgram_ObjTypes);

    return program->getID();
}

GrGLuint GR_GL_FUNCTION_TYPE debugGLCreateShader(GrGLenum type) {
    
    GrAlwaysAssert(GR_GL_VERTEX_SHADER == type || GR_GL_FRAGMENT_SHADER == type);

    GrShaderObj *shader = GR_CREATE(GrShaderObj, GrDebugGL::kShader_ObjTypes);
    shader->setType(type);

    return shader->getID();
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLDeleteProgram(GrGLuint programID) {

    GrProgramObj *program = GR_FIND(programID, GrProgramObj, GrDebugGL::kProgram_ObjTypes);
    GrAlwaysAssert(program);

    if (program->getRefCount()) {
        // someone is still using this program so we can't delete it here
        program->setMarkedForDeletion();
    } else {
        program->deleteAction();
    }
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLDeleteShader(GrGLuint shaderID) {

    GrShaderObj *shader = GR_FIND(shaderID, GrShaderObj, GrDebugGL::kShader_ObjTypes);
    GrAlwaysAssert(shader);

    if (shader->getRefCount()) {
        // someone is still using this shader so we can't delete it here
        shader->setMarkedForDeletion();
    } else {
        shader->deleteAction();
    }
}

// same function used for all glGen*(GLsize i, GLuint*) functions
GrGLvoid GR_GL_FUNCTION_TYPE debugGLGenIds(GrGLsizei n, GrGLuint* ids) {
    static int gCurrID = 1;
    for (int i = 0; i < n; ++i) {
        ids[i] = ++gCurrID;
    }
}

GrGLvoid debugGenObjs(GrDebugGL::GrObjTypes type, GrGLsizei n, GrGLuint* ids) {

   for (int i = 0; i < n; ++i) {
        GrFakeRefObj *obj = GrDebugGL::getInstance()->createObj(type);
        GrAlwaysAssert(obj);
        ids[i] = obj->getID();
    }
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLGenBuffers(GrGLsizei n, GrGLuint* ids) {

    debugGenObjs(GrDebugGL::kBuffer_ObjTypes, n, ids);
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLGenFramebuffers(GrGLsizei n, GrGLuint* ids) {

    debugGenObjs(GrDebugGL::kFrameBuffer_ObjTypes, n, ids);
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLGenRenderbuffers(GrGLsizei n, GrGLuint* ids) {

    debugGenObjs(GrDebugGL::kRenderBuffer_ObjTypes, n, ids);
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLGenTextures(GrGLsizei n, GrGLuint* ids) {

    debugGenObjs(GrDebugGL::kTexture_ObjTypes, n, ids);
}

// same delete function for all glDelete*(GLsize i, const GLuint*) except buffers
GrGLvoid GR_GL_FUNCTION_TYPE debugGLDeleteIds(GrGLsizei n, const GrGLuint* ids) {}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLBindBuffer(GrGLenum target, GrGLuint bufferID) {

    GrAlwaysAssert(GR_GL_ARRAY_BUFFER == target || GR_GL_ELEMENT_ARRAY_BUFFER == target);

    GrBufferObj *buffer = GR_FIND(bufferID, GrBufferObj, GrDebugGL::kBuffer_ObjTypes);
    // 0 is a permissable bufferID - it unbinds the current buffer

    switch (target) {
        case GR_GL_ARRAY_BUFFER:
            GrDebugGL::getInstance()->setArrayBuffer(buffer);
            break;
        case GR_GL_ELEMENT_ARRAY_BUFFER:
            GrDebugGL::getInstance()->setElementArrayBuffer(buffer);
            break;
        default:
            GrCrash("Unexpected target to glBindBuffer");
            break;
    }
}

// deleting a bound buffer has the side effect of binding 0
GrGLvoid GR_GL_FUNCTION_TYPE debugGLDeleteBuffers(GrGLsizei n, const GrGLuint* ids) {
    // first potentially unbind the buffers
    for (int i = 0; i < n; ++i) {

        if (GrDebugGL::getInstance()->getArrayBuffer() && 
            ids[i] == GrDebugGL::getInstance()->getArrayBuffer()->getID()) {
            // this ID is the current array buffer
            GrDebugGL::getInstance()->setArrayBuffer(NULL);
        }
        if (GrDebugGL::getInstance()->getElementArrayBuffer() && 
            ids[i] == GrDebugGL::getInstance()->getElementArrayBuffer()->getID()) {
            // this ID is the current element array buffer
            GrDebugGL::getInstance()->setElementArrayBuffer(NULL);
        }
    }

    // then actually "delete" the buffers
    for (int i = 0; i < n; ++i) {
        GrBufferObj *buffer = GR_FIND(ids[i], GrBufferObj, GrDebugGL::kBuffer_ObjTypes);
        GrAlwaysAssert(buffer);

        GrAlwaysAssert(!buffer->getDeleted());
        buffer->deleteAction();
    }
}

// map a buffer to the caller's address space
GrGLvoid* GR_GL_FUNCTION_TYPE debugGLMapBuffer(GrGLenum target, GrGLenum access) {

    GrAlwaysAssert(GR_GL_ARRAY_BUFFER == target || GR_GL_ELEMENT_ARRAY_BUFFER == target);
    GrAlwaysAssert(GR_GL_WRITE_ONLY == access); // GR_GL_READ_ONLY == access ||  || GR_GL_READ_WRIT == access);

    GrBufferObj *buffer = NULL;
    switch (target) {
        case GR_GL_ARRAY_BUFFER:
            buffer = GrDebugGL::getInstance()->getArrayBuffer();
            break;
        case GR_GL_ELEMENT_ARRAY_BUFFER:
            buffer = GrDebugGL::getInstance()->getElementArrayBuffer();
            break;
        default:
            GrCrash("Unexpected target to glMapBuffer");
            break;
    }

    if (buffer) {
        GrAlwaysAssert(!buffer->getMapped());
        buffer->setMapped();
        return buffer->getDataPtr();
    }

    GrAlwaysAssert(false);
    return NULL;        // no buffer bound to the target
}

// remove a buffer from the caller's address space
// TODO: check if the "access" method from "glMapBuffer" was honored 
GrGLboolean GR_GL_FUNCTION_TYPE debugGLUnmapBuffer(GrGLenum target) {

    GrAlwaysAssert(GR_GL_ARRAY_BUFFER == target || GR_GL_ELEMENT_ARRAY_BUFFER == target);

    GrBufferObj *buffer = NULL;
    switch (target) {
        case GR_GL_ARRAY_BUFFER:
            buffer = GrDebugGL::getInstance()->getArrayBuffer();
            break;
        case GR_GL_ELEMENT_ARRAY_BUFFER:
            buffer = GrDebugGL::getInstance()->getElementArrayBuffer();
            break;
        default:
            GrCrash("Unexpected target to glUnmapBuffer");
            break;
    }

    if (buffer) {
        GrAlwaysAssert(buffer->getMapped());
        buffer->resetMapped();
        return GR_GL_TRUE;
    }

    GrAlwaysAssert(false);
    return GR_GL_FALSE; // GR_GL_INVALID_OPERATION;
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLGetBufferParameteriv(GrGLenum target, GrGLenum value, GrGLint* params) {

    GrAlwaysAssert(GR_GL_ARRAY_BUFFER == target || GR_GL_ELEMENT_ARRAY_BUFFER == target);
    GrAlwaysAssert(GR_GL_BUFFER_SIZE == value || GR_GL_BUFFER_USAGE == value);

    GrBufferObj *buffer = NULL;
    switch (target) {
        case GR_GL_ARRAY_BUFFER:
            buffer = GrDebugGL::getInstance()->getArrayBuffer();
            break;
        case GR_GL_ELEMENT_ARRAY_BUFFER:
            buffer = GrDebugGL::getInstance()->getElementArrayBuffer();
            break;  
    }

    GrAlwaysAssert(buffer);

    switch (value) {
        case GR_GL_BUFFER_MAPPED:
            *params = GR_GL_FALSE;
            if (buffer)
                *params = buffer->getMapped() ? GR_GL_TRUE : GR_GL_FALSE;
            break;
        case GR_GL_BUFFER_SIZE:
            *params = 0;
            if (buffer)
                *params = buffer->getSize();
            break;
        case GR_GL_BUFFER_USAGE:
            *params = GR_GL_STATIC_DRAW;
            if (buffer)
                *params = buffer->getUsage();
            break;
        default:
            GrCrash("Unexpected value to glGetBufferParamateriv");
            break;
    }
};

GrGLenum GR_GL_FUNCTION_TYPE debugGLGetError() {
    return GR_GL_NO_ERROR;
}

GrGLvoid GR_GL_FUNCTION_TYPE debugGLGetIntegerv(GrGLenum pname, GrGLint* params) {
    // TODO: remove from Ganesh the #defines for gets we don't use.
    // We would like to minimize gets overall due to performance issues
    switch (pname) {
        case GR_GL_STENCIL_BITS:
            *params = 8;
            break;
        case GR_GL_SAMPLES:
            *params = 1;
            break;
        case GR_GL_FRAMEBUFFER_BINDING:
            *params = 0;
            break;
        case GR_GL_VIEWPORT:
            params[0] = 0;
            params[1] = 0;
            params[2] = 800;
            params[3] = 600;
            break;
        case GR_GL_MAX_TEXTURE_IMAGE_UNITS:
            *params = 8;
            break;
        case GR_GL_MAX_VERTEX_UNIFORM_VECTORS:
            *params = kDefaultMaxVertexUniformVectors;
            break;
        case GR_GL_MAX_FRAGMENT_UNIFORM_VECTORS:
            *params = kDefaultMaxFragmentUniformVectors;
            break;
        case GR_GL_MAX_FRAGMENT_UNIFORM_COMPONENTS:
            *params = 16 * 4;
            break;
        case GR_GL_NUM_COMPRESSED_TEXTURE_FORMATS:
            *params = 0;
            break;
        case GR_GL_COMPRESSED_TEXTURE_FORMATS:
            break;
        case GR_GL_MAX_TEXTURE_SIZE:
            *params = 8192;
            break;
        case GR_GL_MAX_RENDERBUFFER_SIZE:
            *params = 8192;
            break;
        case GR_GL_MAX_SAMPLES:
            *params = 32;
            break;
        case GR_GL_MAX_VERTEX_ATTRIBS:
            *params = kDefaultMaxVertexAttribs;
            break;
        case GR_GL_MAX_VARYING_VECTORS:
            *params = kDefaultMaxVaryingVectors;
            break;
        case GR_GL_MAX_TEXTURE_UNITS:
            *params = GrDebugGL::getInstance()->getMaxTextureUnits();
            break;
        default:
            GrCrash("Unexpected pname to GetIntegerv");
    }
}
// used for both the program and shader info logs
GrGLvoid GR_GL_FUNCTION_TYPE debugGLGetInfoLog(GrGLuint program, GrGLsizei bufsize, GrGLsizei* length, char* infolog) {
    if (length) {
        *length = 0;
    }
    if (bufsize > 0) {
        *infolog = 0;
    }
}

// used for both the program and shader params
GrGLvoid GR_GL_FUNCTION_TYPE debugGLGetShaderOrProgramiv(GrGLuint program, GrGLenum pname, GrGLint* params) {
    switch (pname) {
        case GR_GL_LINK_STATUS:  // fallthru
        case GR_GL_COMPILE_STATUS:
            *params = GR_GL_TRUE;
            break;
        case GR_GL_INFO_LOG_LENGTH:
            *params = 0;
            break;
        // we don't expect any other pnames
        default:
            GrCrash("Unexpected pname to GetProgramiv");
            break;
    }
}

namespace {
template <typename T>
void query_result(GrGLenum GLtarget, GrGLenum pname, T *params) {
    switch (pname) {
        case GR_GL_QUERY_RESULT_AVAILABLE:
            *params = GR_GL_TRUE;
            break;
        case GR_GL_QUERY_RESULT:
            *params = 0;
            break;
        default:
            GrCrash("Unexpected pname passed to GetQueryObject.");
            break;
    }
}
}

// Queries on the null GL just don't do anything at all. We could potentially make
// the timers work.
GrGLvoid GR_GL_FUNCTION_TYPE debugGLGetQueryiv(GrGLenum GLtarget, GrGLenum pname, GrGLint *params) {
    switch (pname) {
        case GR_GL_CURRENT_QUERY:
            *params = 0;
            break;
        case GR_GL_QUERY_COUNTER_BITS:
            *params = 32;
            break;
        default:
            GrCrash("Unexpected pname passed GetQueryiv.");
    }
}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLGetQueryObjecti64v(GrGLuint id, GrGLenum pname, GrGLint64 *params) {
    query_result(id, pname, params);
}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLGetQueryObjectiv(GrGLuint id, GrGLenum pname, GrGLint *params) {
    query_result(id, pname, params);
}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLGetQueryObjectui64v(GrGLuint id, GrGLenum pname, GrGLuint64 *params) {
    query_result(id, pname, params);
}
GrGLvoid GR_GL_FUNCTION_TYPE debugGLGetQueryObjectuiv(GrGLuint id, GrGLenum pname, GrGLuint *params) {
    query_result(id, pname, params);
}

const GrGLubyte* GR_GL_FUNCTION_TYPE debugGLGetString(GrGLenum name) {
    switch (name) {
        case GR_GL_EXTENSIONS:
            return (const GrGLubyte*)"GL_ARB_framebuffer_object GL_ARB_blend_func_extended GL_ARB_timer_query GL_ARB_draw_buffers GL_ARB_occlusion_query GL_EXT_blend_color GL_EXT_stencil_wrap";
        case GR_GL_VERSION:
            return (const GrGLubyte*)"4.0 Null GL";
        case GR_GL_SHADING_LANGUAGE_VERSION:
            return (const GrGLubyte*)"4.20.8 Null GLSL";
        default:
            GrCrash("Unexpected name to GetString");
            return NULL;
    }
}

// we used to use this to query stuff about externally created textures, now we just
// require clients to tell us everything about the texture.
GrGLvoid GR_GL_FUNCTION_TYPE debugGLGetTexLevelParameteriv(GrGLenum target, GrGLint level, GrGLenum pname, GrGLint* params) {
    GrCrash("Should never query texture parameters.");
}

GrGLint GR_GL_FUNCTION_TYPE debugGLGetUniformLocation(GrGLuint program, const char* name) {
    static int gUniLocation = 0;
    return ++gUniLocation;
}

////////////////////////////////////////////////////////////////////////////////
struct GrDebugGLInterface : public GrGLInterface {

public:
    GrDebugGLInterface()
        : fWrapped(NULL) {
    }

    void setWrapped(GrGLInterface *interface) {
        fWrapped.reset(interface);
    }

    // TODO: there are some issues w/ wrapping another GL interface inside the
    // debug interface:
    //      Since none of the "gl" methods are member functions they don't get
    //      a "this" pointer through which to access "fWrapped"
    //      This could be worked around by having all of them access the 
    //      "glInterface" pointer - i.e., treating the debug interface as a 
    //      true singleton
    //
    //      The problem with this is that we also want to handle OpenGL 
    //      contexts. The natural way to do this is to have multiple debug 
    //      interfaces. Each of which represents a separate context. The 
    //      static ID count would still uniquify IDs across all of them. 
    //      The problem then is that we couldn't treat the debug GL 
    //      interface as a singleton (since there would be one for each 
    //      context).
    //
    //      The solution to this is probably to alter SkDebugGlContext's 
    //      "makeCurrent" method to make a call like "makeCurrent(this)" to 
    //      the debug GL interface (assuming that the application will create 
    //      multiple SkGLContext's) to let it switch between the active 
    //      context. Everything in the GrDebugGL object would then need to be 
    //      moved to a GrContextObj and the GrDebugGL object would just switch 
    //      between them. Note that this approach would also require that 
    //      SkDebugGLContext wrap an arbitrary other context
    //      and then pass the wrapped interface to the debug GL interface.

protected:
private:

    SkAutoTUnref<GrGLInterface> fWrapped;

    typedef GrGLInterface INHERITED;
};

////////////////////////////////////////////////////////////////////////////////
const GrGLInterface* GrGLCreateDebugInterface() {
    // The gl functions are not context-specific so we create one global 
    // interface
    static SkAutoTUnref<GrGLInterface> glInterface;
    if (!glInterface.get()) {
        GrGLInterface* interface = new GrDebugGLInterface;
        glInterface.reset(interface);
        interface->fBindingsExported = kDesktop_GrGLBinding;
        interface->fActiveTexture = debugGLActiveTexture;
        interface->fAttachShader = debugGLAttachShader;
        interface->fBeginQuery = debugGLBeginQuery;
        interface->fBindAttribLocation = debugGLBindAttribLocation;
        interface->fBindBuffer = debugGLBindBuffer;
        interface->fBindFragDataLocation = debugGLBindFragDataLocation;
        interface->fBindTexture = debugGLBindTexture;
        interface->fBlendColor = debugGLBlendColor;
        interface->fBlendFunc = debugGLBlendFunc;
        interface->fBufferData = debugGLBufferData;
        interface->fBufferSubData = debugGLBufferSubData;
        interface->fClear = debugGLClear;
        interface->fClearColor = debugGLClearColor;
        interface->fClearStencil = debugGLClearStencil;
        interface->fColorMask = debugGLColorMask;
        interface->fCompileShader = debugGLCompileShader;
        interface->fCompressedTexImage2D = debugGLCompressedTexImage2D;
        interface->fCreateProgram = debugGLCreateProgram;
        interface->fCreateShader = debugGLCreateShader;
        interface->fCullFace = debugGLCullFace;
        interface->fDeleteBuffers = debugGLDeleteBuffers;
        interface->fDeleteProgram = debugGLDeleteProgram;
        interface->fDeleteQueries = debugGLDeleteIds;
        interface->fDeleteShader = debugGLDeleteShader;
        interface->fDeleteTextures = debugGLDeleteTextures;
        interface->fDepthMask = debugGLDepthMask;
        interface->fDisable = debugGLDisable;
        interface->fDisableVertexAttribArray = debugGLDisableVertexAttribArray;
        interface->fDrawArrays = debugGLDrawArrays;
        interface->fDrawBuffer = debugGLDrawBuffer;
        interface->fDrawBuffers = debugGLDrawBuffers;
        interface->fDrawElements = debugGLDrawElements;
        interface->fEnable = debugGLEnable;
        interface->fEnableVertexAttribArray = debugGLEnableVertexAttribArray;
        interface->fEndQuery = debugGLEndQuery;
        interface->fFinish = debugGLFinish;
        interface->fFlush = debugGLFlush;
        interface->fFrontFace = debugGLFrontFace;
        interface->fGenBuffers = debugGLGenBuffers;
        interface->fGenQueries = debugGLGenIds;
        interface->fGenTextures = debugGLGenTextures;
        interface->fGetBufferParameteriv = debugGLGetBufferParameteriv;
        interface->fGetError = debugGLGetError;
        interface->fGetIntegerv = debugGLGetIntegerv;
        interface->fGetQueryObjecti64v = debugGLGetQueryObjecti64v;
        interface->fGetQueryObjectiv = debugGLGetQueryObjectiv;
        interface->fGetQueryObjectui64v = debugGLGetQueryObjectui64v;
        interface->fGetQueryObjectuiv = debugGLGetQueryObjectuiv;
        interface->fGetQueryiv = debugGLGetQueryiv;
        interface->fGetProgramInfoLog = debugGLGetInfoLog;
        interface->fGetProgramiv = debugGLGetShaderOrProgramiv;
        interface->fGetShaderInfoLog = debugGLGetInfoLog;
        interface->fGetShaderiv = debugGLGetShaderOrProgramiv;
        interface->fGetString = debugGLGetString;
        interface->fGetTexLevelParameteriv = debugGLGetTexLevelParameteriv;
        interface->fGetUniformLocation = debugGLGetUniformLocation;
        interface->fLineWidth = debugGLLineWidth;
        interface->fLinkProgram = debugGLLinkProgram;
        interface->fPixelStorei = debugGLPixelStorei;
        interface->fQueryCounter = debugGLQueryCounter;
        interface->fReadBuffer = debugGLReadBuffer;
        interface->fReadPixels = debugGLReadPixels;
        interface->fScissor = debugGLScissor;
        interface->fShaderSource = debugGLShaderSource;
        interface->fStencilFunc = debugGLStencilFunc;
        interface->fStencilFuncSeparate = debugGLStencilFuncSeparate;
        interface->fStencilMask = debugGLStencilMask;
        interface->fStencilMaskSeparate = debugGLStencilMaskSeparate;
        interface->fStencilOp = debugGLStencilOp;
        interface->fStencilOpSeparate = debugGLStencilOpSeparate;
        interface->fTexImage2D = debugGLTexImage2D;
        interface->fTexParameteri = debugGLTexParameteri;
        interface->fTexSubImage2D = debugGLTexSubImage2D;
        interface->fTexStorage2D = debugGLTexStorage2D;
        interface->fUniform1f = debugGLUniform1f;
        interface->fUniform1i = debugGLUniform1i;
        interface->fUniform1fv = debugGLUniform1fv;
        interface->fUniform1iv = debugGLUniform1iv;
        interface->fUniform2f = debugGLUniform2f;
        interface->fUniform2i = debugGLUniform2i;
        interface->fUniform2fv = debugGLUniform2fv;
        interface->fUniform2iv = debugGLUniform2iv;
        interface->fUniform3f = debugGLUniform3f;
        interface->fUniform3i = debugGLUniform3i;
        interface->fUniform3fv = debugGLUniform3fv;
        interface->fUniform3iv = debugGLUniform3iv;
        interface->fUniform4f = debugGLUniform4f;
        interface->fUniform4i = debugGLUniform4i;
        interface->fUniform4fv = debugGLUniform4fv;
        interface->fUniform4iv = debugGLUniform4iv;
        interface->fUniformMatrix2fv = debugGLUniformMatrix2fv;
        interface->fUniformMatrix3fv = debugGLUniformMatrix3fv;
        interface->fUniformMatrix4fv = debugGLUniformMatrix4fv;
        interface->fUseProgram = debugGLUseProgram;
        interface->fVertexAttrib4fv = debugGLVertexAttrib4fv;
        interface->fVertexAttribPointer = debugGLVertexAttribPointer;
        interface->fViewport = debugGLViewport;
        interface->fBindFramebuffer = debugGLBindFramebuffer;
        interface->fBindRenderbuffer = debugGLBindRenderbuffer;
        interface->fCheckFramebufferStatus = debugGLCheckFramebufferStatus;
        interface->fDeleteFramebuffers = debugGLDeleteFramebuffers;
        interface->fDeleteRenderbuffers = debugGLDeleteRenderbuffers;
        interface->fFramebufferRenderbuffer = debugGLFramebufferRenderbuffer;
        interface->fFramebufferTexture2D = debugGLFramebufferTexture2D;
        interface->fGenFramebuffers = debugGLGenFramebuffers;
        interface->fGenRenderbuffers = debugGLGenRenderbuffers;
        interface->fGetFramebufferAttachmentParameteriv = debugGLGetFramebufferAttachmentParameteriv;
        interface->fGetRenderbufferParameteriv = debugGLGetRenderbufferParameteriv;
        interface->fRenderbufferStorage = debugGLRenderbufferStorage;
        interface->fRenderbufferStorageMultisample = debugGLRenderbufferStorageMultisample;
        interface->fBlitFramebuffer = debugGLBlitFramebuffer;
        interface->fResolveMultisampleFramebuffer = debugGLResolveMultisampleFramebuffer;
        interface->fMapBuffer = debugGLMapBuffer;
        interface->fUnmapBuffer = debugGLUnmapBuffer;
        interface->fBindFragDataLocationIndexed = debugGLBindFragDataLocationIndexed;
    }
    glInterface.get()->ref();
    return glInterface.get();
}