1dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
2dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com/*
3dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com * Copyright 2012 Google Inc.
4dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com *
5dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com * Use of this source code is governed by a BSD-style license that can be
6dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com * found in the LICENSE file.
7dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com */
8dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
9dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com#ifndef GrDebugGL_DEFINED
10dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com#define GrDebugGL_DEFINED
11dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
12dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com#include "SkTArray.h"
13dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com#include "gl/GrGLInterface.h"
14dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
15b82c3993c2f753e5caaff2df9648ebdafc68595fbsalomon@google.comclass GrBufferObj;
16ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.comclass GrFakeRefObj;
17dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.comclass GrFrameBufferObj;
18b82c3993c2f753e5caaff2df9648ebdafc68595fbsalomon@google.comclass GrProgramObj;
19ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.comclass GrRenderBufferObj;
20ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.comclass GrTextureObj;
21ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.comclass GrTextureUnitObj;
22ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.comclass GrVertexArrayObj;
23dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
24dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com////////////////////////////////////////////////////////////////////////////////
25dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com// This is the main debugging object. It is a singleton and keeps track of
26dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com// all the other debug objects.
27dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.comclass GrDebugGL {
28dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.compublic:
29dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    enum GrObjTypes {
30dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        kTexture_ObjTypes = 0,
31dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        kBuffer_ObjTypes,
32dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        kRenderBuffer_ObjTypes,
33dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        kFrameBuffer_ObjTypes,
34dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        kShader_ObjTypes,
35dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        kProgram_ObjTypes,
36dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        kTextureUnit_ObjTypes,
37ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.com        kVertexArray_ObjTypes,
38dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        kObjTypeCount
39dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    };
40dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
41dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    GrFakeRefObj *createObj(GrObjTypes type) {
42dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        GrFakeRefObj *temp = (*gFactoryFunc[type])();
43dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
44dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        fObjects.push_back(temp);
45dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
46dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        return temp;
47dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    }
48dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
49dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    GrFakeRefObj *findObject(GrGLuint ID, GrObjTypes type);
50dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
51dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    GrGLuint getMaxTextureUnits() const { return kDefaultMaxTextureUnits; }
52dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
53dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    void setCurTextureUnit(GrGLuint curTextureUnit) { fCurTextureUnit = curTextureUnit; }
54dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    GrGLuint getCurTextureUnit() const { return fCurTextureUnit; }
55dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
56dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    GrTextureUnitObj *getTextureUnit(int iUnit) {
57dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        GrAlwaysAssert(0 <= iUnit && kDefaultMaxTextureUnits > iUnit);
58dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
59dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com        return fTextureUnits[iUnit];
60dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    }
61dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
62dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    void setArrayBuffer(GrBufferObj *arrayBuffer);
63dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    GrBufferObj *getArrayBuffer()                   { return fArrayBuffer; }
64dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
65dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    void setElementArrayBuffer(GrBufferObj *elementArrayBuffer);
66dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    GrBufferObj *getElementArrayBuffer()                            { return fElementArrayBuffer; }
67dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
68ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.com    void setVertexArray(GrVertexArrayObj* vertexArray);
69ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.com    GrVertexArrayObj* getVertexArray() { return fVertexArray; }
70ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.com
71dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    void setTexture(GrTextureObj *texture);
72dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
73dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    void setFrameBuffer(GrFrameBufferObj *frameBuffer);
74dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    GrFrameBufferObj *getFrameBuffer()                  { return fFrameBuffer; }
75dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
76dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    void setRenderBuffer(GrRenderBufferObj *renderBuffer);
77dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    GrRenderBufferObj *getRenderBuffer()                  { return fRenderBuffer; }
78dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
79dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    void useProgram(GrProgramObj *program);
80dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
81fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    void setPackRowLength(GrGLint packRowLength) {
82fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        fPackRowLength = packRowLength;
83670ff9ae7f37356bff08b30a356bb0c52dc8d62erobertphillips@google.com    }
84670ff9ae7f37356bff08b30a356bb0c52dc8d62erobertphillips@google.com    GrGLint getPackRowLength() const { return fPackRowLength; }
85670ff9ae7f37356bff08b30a356bb0c52dc8d62erobertphillips@google.com
86670ff9ae7f37356bff08b30a356bb0c52dc8d62erobertphillips@google.com    void setUnPackRowLength(GrGLint unPackRowLength) {
87670ff9ae7f37356bff08b30a356bb0c52dc8d62erobertphillips@google.com        fUnPackRowLength = unPackRowLength;
88670ff9ae7f37356bff08b30a356bb0c52dc8d62erobertphillips@google.com    }
89670ff9ae7f37356bff08b30a356bb0c52dc8d62erobertphillips@google.com    GrGLint getUnPackRowLength() const { return fUnPackRowLength; }
90670ff9ae7f37356bff08b30a356bb0c52dc8d62erobertphillips@google.com
91dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    static GrDebugGL *getInstance() {
92622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com        // someone should admit to actually using this class
93f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(0 < gStaticRefCount);
94dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
952880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        if (nullptr == gObj) {
962880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            gObj = new GrDebugGL;
97622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com        }
98622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com
99622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com        return gObj;
100dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    }
101dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
102dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    void report() const;
103dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
104622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com    static void staticRef() {
105622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com        gStaticRefCount++;
106622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com    }
107622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com
108622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com    static void staticUnRef() {
109f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(gStaticRefCount > 0);
110622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com        gStaticRefCount--;
111622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com        if (0 == gStaticRefCount) {
1122880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            delete gObj;
1132880df2609eba09b555ca37be04b6ad89290c765Tom Hudson            gObj = nullptr;
114622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com        }
115622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com    }
116622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com
117944bcf0acb78f8f9da01e991f1307629959e338absalomon    static void abandon() {
118944bcf0acb78f8f9da01e991f1307629959e338absalomon        SkASSERT(gStaticRefCount > 0);
119944bcf0acb78f8f9da01e991f1307629959e338absalomon        gObj->fAbandoned = true;
120944bcf0acb78f8f9da01e991f1307629959e338absalomon    }
121944bcf0acb78f8f9da01e991f1307629959e338absalomon
122dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.comprotected:
123dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
124dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.comprivate:
125dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    // the OpenGLES 2.0 spec says this must be >= 2
126dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    static const GrGLint kDefaultMaxTextureUnits = 8;
127dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
128670ff9ae7f37356bff08b30a356bb0c52dc8d62erobertphillips@google.com    GrGLint         fPackRowLength;
129670ff9ae7f37356bff08b30a356bb0c52dc8d62erobertphillips@google.com    GrGLint         fUnPackRowLength;
130dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    GrGLuint        fCurTextureUnit;
131ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.com    GrBufferObj*    fArrayBuffer;
132ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.com    GrBufferObj*    fElementArrayBuffer;
133ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.com    GrFrameBufferObj* fFrameBuffer;
134ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.com    GrRenderBufferObj* fRenderBuffer;
135ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.com    GrProgramObj* fProgram;
136ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.com    GrTextureObj* fTexture;
137dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    GrTextureUnitObj *fTextureUnits[kDefaultMaxTextureUnits];
138ecd84842b3f65918eb040c53391172b6413fd7adbsalomon@google.com    GrVertexArrayObj *fVertexArray;
139dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
140944bcf0acb78f8f9da01e991f1307629959e338absalomon    bool fAbandoned;
141944bcf0acb78f8f9da01e991f1307629959e338absalomon
142dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    typedef GrFakeRefObj *(*Create)();
143dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
144dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    static Create gFactoryFunc[kObjTypeCount];
145dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
146622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com    static GrDebugGL* gObj;
147622a17091b69df8d54d318c88f34851677a6d9c2robertphillips@google.com    static int gStaticRefCount;
148dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
149dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    // global store of all objects
150dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    SkTArray<GrFakeRefObj *> fObjects;
151dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
152dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    GrDebugGL();
153dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    ~GrDebugGL();
154dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com};
155dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
156dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com////////////////////////////////////////////////////////////////////////////////
157dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com// Helper macro to make creating an object (where you need to get back a derived
158dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com// type) easier
159dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com#define GR_CREATE(className, classEnum)                     \
160dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    reinterpret_cast<className *>(GrDebugGL::getInstance()->createObj(classEnum))
161dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
162dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com////////////////////////////////////////////////////////////////////////////////
163dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com// Helper macro to make finding objects less painful
164dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com#define GR_FIND(id, className, classEnum)                   \
165dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com    reinterpret_cast<className *>(GrDebugGL::getInstance()->findObject(id, classEnum))
166dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com
167dd743fefad9764ad86d7f69deec32e9a3b5de47frobertphillips@google.com#endif // GrDebugGL_DEFINED
168