1 
2 /*
3  * Copyright 2012 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 #ifndef GrDebugGL_DEFINED
10 #define GrDebugGL_DEFINED
11 
12 #include "SkTArray.h"
13 #include "gl/GrGLInterface.h"
14 
15 class GrBufferObj;
16 class GrFakeRefObj;
17 class GrFrameBufferObj;
18 class GrProgramObj;
19 class GrRenderBufferObj;
20 class GrTextureObj;
21 class GrTextureUnitObj;
22 class GrVertexArrayObj;
23 
24 ////////////////////////////////////////////////////////////////////////////////
25 // This is the main debugging object. It is a singleton and keeps track of
26 // all the other debug objects.
27 class GrDebugGL {
28 public:
29     enum GrObjTypes {
30         kTexture_ObjTypes = 0,
31         kBuffer_ObjTypes,
32         kRenderBuffer_ObjTypes,
33         kFrameBuffer_ObjTypes,
34         kShader_ObjTypes,
35         kProgram_ObjTypes,
36         kTextureUnit_ObjTypes,
37         kVertexArray_ObjTypes,
38         kObjTypeCount
39     };
40 
createObj(GrObjTypes type)41     GrFakeRefObj *createObj(GrObjTypes type) {
42         GrFakeRefObj *temp = (*gFactoryFunc[type])();
43 
44         fObjects.push_back(temp);
45 
46         return temp;
47     }
48 
49     GrFakeRefObj *findObject(GrGLuint ID, GrObjTypes type);
50 
getMaxTextureUnits()51     GrGLuint getMaxTextureUnits() const { return kDefaultMaxTextureUnits; }
52 
setCurTextureUnit(GrGLuint curTextureUnit)53     void setCurTextureUnit(GrGLuint curTextureUnit) { fCurTextureUnit = curTextureUnit; }
getCurTextureUnit()54     GrGLuint getCurTextureUnit() const { return fCurTextureUnit; }
55 
getTextureUnit(int iUnit)56     GrTextureUnitObj *getTextureUnit(int iUnit) {
57         GrAlwaysAssert(0 <= iUnit && kDefaultMaxTextureUnits > iUnit);
58 
59         return fTextureUnits[iUnit];
60     }
61 
62     void setArrayBuffer(GrBufferObj *arrayBuffer);
getArrayBuffer()63     GrBufferObj *getArrayBuffer()                   { return fArrayBuffer; }
64 
65     void setElementArrayBuffer(GrBufferObj *elementArrayBuffer);
getElementArrayBuffer()66     GrBufferObj *getElementArrayBuffer()                            { return fElementArrayBuffer; }
67 
68     void setVertexArray(GrVertexArrayObj* vertexArray);
getVertexArray()69     GrVertexArrayObj* getVertexArray() { return fVertexArray; }
70 
71     void setTexture(GrTextureObj *texture);
72 
73     void setFrameBuffer(GrFrameBufferObj *frameBuffer);
getFrameBuffer()74     GrFrameBufferObj *getFrameBuffer()                  { return fFrameBuffer; }
75 
76     void setRenderBuffer(GrRenderBufferObj *renderBuffer);
getRenderBuffer()77     GrRenderBufferObj *getRenderBuffer()                  { return fRenderBuffer; }
78 
79     void useProgram(GrProgramObj *program);
80 
setPackRowLength(GrGLint packRowLength)81     void setPackRowLength(GrGLint packRowLength) {
82         fPackRowLength = packRowLength;
83     }
getPackRowLength()84     GrGLint getPackRowLength() const { return fPackRowLength; }
85 
setUnPackRowLength(GrGLint unPackRowLength)86     void setUnPackRowLength(GrGLint unPackRowLength) {
87         fUnPackRowLength = unPackRowLength;
88     }
getUnPackRowLength()89     GrGLint getUnPackRowLength() const { return fUnPackRowLength; }
90 
getInstance()91     static GrDebugGL *getInstance() {
92         // someone should admit to actually using this class
93         SkASSERT(0 < gStaticRefCount);
94 
95         if (nullptr == gObj) {
96             gObj = new GrDebugGL;
97         }
98 
99         return gObj;
100     }
101 
102     void report() const;
103 
staticRef()104     static void staticRef() {
105         gStaticRefCount++;
106     }
107 
staticUnRef()108     static void staticUnRef() {
109         SkASSERT(gStaticRefCount > 0);
110         gStaticRefCount--;
111         if (0 == gStaticRefCount) {
112             delete gObj;
113             gObj = nullptr;
114         }
115     }
116 
abandon()117     static void abandon() {
118         SkASSERT(gStaticRefCount > 0);
119         gObj->fAbandoned = true;
120     }
121 
122 protected:
123 
124 private:
125     // the OpenGLES 2.0 spec says this must be >= 2
126     static const GrGLint kDefaultMaxTextureUnits = 8;
127 
128     GrGLint         fPackRowLength;
129     GrGLint         fUnPackRowLength;
130     GrGLuint        fCurTextureUnit;
131     GrBufferObj*    fArrayBuffer;
132     GrBufferObj*    fElementArrayBuffer;
133     GrFrameBufferObj* fFrameBuffer;
134     GrRenderBufferObj* fRenderBuffer;
135     GrProgramObj* fProgram;
136     GrTextureObj* fTexture;
137     GrTextureUnitObj *fTextureUnits[kDefaultMaxTextureUnits];
138     GrVertexArrayObj *fVertexArray;
139 
140     bool fAbandoned;
141 
142     typedef GrFakeRefObj *(*Create)();
143 
144     static Create gFactoryFunc[kObjTypeCount];
145 
146     static GrDebugGL* gObj;
147     static int gStaticRefCount;
148 
149     // global store of all objects
150     SkTArray<GrFakeRefObj *> fObjects;
151 
152     GrDebugGL();
153     ~GrDebugGL();
154 };
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 // Helper macro to make creating an object (where you need to get back a derived
158 // type) easier
159 #define GR_CREATE(className, classEnum)                     \
160     reinterpret_cast<className *>(GrDebugGL::getInstance()->createObj(classEnum))
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 // Helper macro to make finding objects less painful
164 #define GR_FIND(id, className, classEnum)                   \
165     reinterpret_cast<className *>(GrDebugGL::getInstance()->findObject(id, classEnum))
166 
167 #endif // GrDebugGL_DEFINED
168