180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2012 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef GrDebugGL_DEFINED
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define GrDebugGL_DEFINED
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTArray.h"
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "gl/GrGLInterface.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass GrBufferObj;
16096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenbergerclass GrFakeRefObj;
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass GrFrameBufferObj;
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass GrProgramObj;
19096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenbergerclass GrRenderBufferObj;
20096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenbergerclass GrTextureObj;
21096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenbergerclass GrTextureUnitObj;
22096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenbergerclass GrVertexArrayObj;
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru////////////////////////////////////////////////////////////////////////////////
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// This is the main debugging object. It is a singleton and keeps track of
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// all the other debug objects.
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass GrDebugGL {
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    enum GrObjTypes {
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kTexture_ObjTypes = 0,
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kBuffer_ObjTypes,
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kRenderBuffer_ObjTypes,
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kFrameBuffer_ObjTypes,
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kShader_ObjTypes,
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kProgram_ObjTypes,
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kTextureUnit_ObjTypes,
37096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger        kVertexArray_ObjTypes,
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        kObjTypeCount
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrFakeRefObj *createObj(GrObjTypes type) {
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GrFakeRefObj *temp = (*gFactoryFunc[type])();
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fObjects.push_back(temp);
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return temp;
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrFakeRefObj *findObject(GrGLuint ID, GrObjTypes type);
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLuint getMaxTextureUnits() const { return kDefaultMaxTextureUnits; }
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setCurTextureUnit(GrGLuint curTextureUnit) { fCurTextureUnit = curTextureUnit; }
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLuint getCurTextureUnit() const { return fCurTextureUnit; }
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrTextureUnitObj *getTextureUnit(int iUnit) {
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GrAlwaysAssert(0 <= iUnit && kDefaultMaxTextureUnits > iUnit);
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return fTextureUnits[iUnit];
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setArrayBuffer(GrBufferObj *arrayBuffer);
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrBufferObj *getArrayBuffer()                   { return fArrayBuffer; }
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setElementArrayBuffer(GrBufferObj *elementArrayBuffer);
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrBufferObj *getElementArrayBuffer()                            { return fElementArrayBuffer; }
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
68096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    void setVertexArray(GrVertexArrayObj* vertexArray);
69096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    GrVertexArrayObj* getVertexArray() { return fVertexArray; }
70096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setTexture(GrTextureObj *texture);
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setFrameBuffer(GrFrameBufferObj *frameBuffer);
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrFrameBufferObj *getFrameBuffer()                  { return fFrameBuffer; }
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setRenderBuffer(GrRenderBufferObj *renderBuffer);
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrRenderBufferObj *getRenderBuffer()                  { return fRenderBuffer; }
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void useProgram(GrProgramObj *program);
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setPackRowLength(GrGLint packRowLength) {
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fPackRowLength = packRowLength;
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLint getPackRowLength() const { return fPackRowLength; }
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setUnPackRowLength(GrGLint unPackRowLength) {
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fUnPackRowLength = unPackRowLength;
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLint getUnPackRowLength() const { return fUnPackRowLength; }
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static GrDebugGL *getInstance() {
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // someone should admit to actually using this class
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GrAssert(0 < gStaticRefCount);
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (NULL == gObj) {
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            gObj = SkNEW(GrDebugGL);
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return gObj;
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void report() const;
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static void staticRef() {
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        gStaticRefCount++;
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static void staticUnRef() {
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GrAssert(gStaticRefCount > 0);
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        gStaticRefCount--;
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (0 == gStaticRefCount) {
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkDELETE(gObj);
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            gObj = NULL;
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // the OpenGLES 2.0 spec says this must be >= 2
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static const GrGLint kDefaultMaxTextureUnits = 8;
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLint         fPackRowLength;
12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLint         fUnPackRowLength;
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrGLuint        fCurTextureUnit;
126096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    GrBufferObj*    fArrayBuffer;
127096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    GrBufferObj*    fElementArrayBuffer;
128096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    GrFrameBufferObj* fFrameBuffer;
129096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    GrRenderBufferObj* fRenderBuffer;
130096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    GrProgramObj* fProgram;
131096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    GrTextureObj* fTexture;
13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrTextureUnitObj *fTextureUnits[kDefaultMaxTextureUnits];
133096defe64d408e54474fe19f418c95bf1a554fc7Derek Sollenberger    GrVertexArrayObj *fVertexArray;
13480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef GrFakeRefObj *(*Create)();
13680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static Create gFactoryFunc[kObjTypeCount];
13880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static GrDebugGL* gObj;
14080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static int gStaticRefCount;
14180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // global store of all objects
14380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTArray<GrFakeRefObj *> fObjects;
14480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    GrDebugGL();
14680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    ~GrDebugGL();
14780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
14880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru////////////////////////////////////////////////////////////////////////////////
15080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// Helper macro to make creating an object (where you need to get back a derived
15180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// type) easier
15280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define GR_CREATE(className, classEnum)                     \
15380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    reinterpret_cast<className *>(GrDebugGL::getInstance()->createObj(classEnum))
15480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru////////////////////////////////////////////////////////////////////////////////
15680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// Helper macro to make finding objects less painful
15780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define GR_FIND(id, className, classEnum)                   \
15880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    reinterpret_cast<className *>(GrDebugGL::getInstance()->findObject(id, classEnum))
15980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif // GrDebugGL_DEFINED
161