Home | History | Annotate | Download | only in debug
      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 #include "GrDebugGL.h"
     10 #include "GrTextureObj.h"
     11 #include "GrBufferObj.h"
     12 #include "GrRenderBufferObj.h"
     13 #include "GrFrameBufferObj.h"
     14 #include "GrShaderObj.h"
     15 #include "GrProgramObj.h"
     16 #include "GrTextureUnitObj.h"
     17 #include "GrVertexArrayObj.h"
     18 
     19 GrDebugGL* GrDebugGL::gObj = NULL;
     20 int GrDebugGL::gStaticRefCount = 0;
     21 GrDebugGL::Create GrDebugGL::gFactoryFunc[kObjTypeCount] = {
     22     GrTextureObj::createGrTextureObj,
     23     GrBufferObj::createGrBufferObj,
     24     GrRenderBufferObj::createGrRenderBufferObj,
     25     GrFrameBufferObj::createGrFrameBufferObj,
     26     GrShaderObj::createGrShaderObj,
     27     GrProgramObj::createGrProgramObj,
     28     GrTextureUnitObj::createGrTextureUnitObj,
     29     GrVertexArrayObj::createGrVertexArrayObj,
     30 };
     31 
     32 
     33 GrDebugGL::GrDebugGL()
     34     : fPackRowLength(0)
     35     , fUnPackRowLength(0)
     36     , fCurTextureUnit(0)
     37     , fArrayBuffer(NULL)
     38     , fElementArrayBuffer(NULL)
     39     , fFrameBuffer(NULL)
     40     , fRenderBuffer(NULL)
     41     , fProgram(NULL)
     42     , fTexture(NULL)
     43     , fVertexArray(NULL)  {
     44 
     45     for (int i = 0; i < kDefaultMaxTextureUnits; ++i) {
     46 
     47         fTextureUnits[i] = reinterpret_cast<GrTextureUnitObj *>(
     48                             createObj(GrDebugGL::kTextureUnit_ObjTypes));
     49         fTextureUnits[i]->ref();
     50 
     51         fTextureUnits[i]->setNumber(i);
     52     }
     53 }
     54 
     55 GrDebugGL::~GrDebugGL() {
     56     // unref & delete the texture units first so they don't show up on the leak report
     57     for (int i = 0; i < kDefaultMaxTextureUnits; ++i) {
     58         fTextureUnits[i]->unref();
     59         fTextureUnits[i]->deleteAction();
     60     }
     61 
     62     this->report();
     63 
     64     for (int i = 0; i < fObjects.count(); ++i) {
     65         delete fObjects[i];
     66     }
     67     fObjects.reset();
     68 
     69     fArrayBuffer = NULL;
     70     fElementArrayBuffer = NULL;
     71     fFrameBuffer = NULL;
     72     fRenderBuffer = NULL;
     73     fProgram = NULL;
     74     fTexture = NULL;
     75     fVertexArray = NULL;
     76 }
     77 
     78 GrFakeRefObj *GrDebugGL::findObject(GrGLuint ID, GrObjTypes type) {
     79     for (int i = 0; i < fObjects.count(); ++i) {
     80         if (fObjects[i]->getID() == ID) { // && fObjects[i]->getType() == type) {
     81             // The application shouldn't be accessing objects
     82             // that (as far as OpenGL knows) were already deleted
     83             GrAlwaysAssert(!fObjects[i]->getDeleted());
     84             GrAlwaysAssert(!fObjects[i]->getMarkedForDeletion());
     85             return fObjects[i];
     86         }
     87     }
     88 
     89     return NULL;
     90 }
     91 
     92 void GrDebugGL::setArrayBuffer(GrBufferObj *arrayBuffer) {
     93     if (fArrayBuffer) {
     94         // automatically break the binding of the old buffer
     95         GrAlwaysAssert(fArrayBuffer->getBound());
     96         fArrayBuffer->resetBound();
     97 
     98         GrAlwaysAssert(!fArrayBuffer->getDeleted());
     99         fArrayBuffer->unref();
    100     }
    101 
    102     fArrayBuffer = arrayBuffer;
    103 
    104     if (fArrayBuffer) {
    105         GrAlwaysAssert(!fArrayBuffer->getDeleted());
    106         fArrayBuffer->ref();
    107 
    108         GrAlwaysAssert(!fArrayBuffer->getBound());
    109         fArrayBuffer->setBound();
    110     }
    111 }
    112 
    113 void GrDebugGL::setVertexArray(GrVertexArrayObj* vertexArray) {
    114     if (NULL != vertexArray) {
    115         SkASSERT(!vertexArray->getDeleted());
    116     }
    117     SkRefCnt_SafeAssign(fVertexArray, vertexArray);
    118 }
    119 
    120 void GrDebugGL::setElementArrayBuffer(GrBufferObj *elementArrayBuffer) {
    121     if (fElementArrayBuffer) {
    122         // automatically break the binding of the old buffer
    123         GrAlwaysAssert(fElementArrayBuffer->getBound());
    124         fElementArrayBuffer->resetBound();
    125 
    126         GrAlwaysAssert(!fElementArrayBuffer->getDeleted());
    127         fElementArrayBuffer->unref();
    128     }
    129 
    130     fElementArrayBuffer = elementArrayBuffer;
    131 
    132     if (fElementArrayBuffer) {
    133         GrAlwaysAssert(!fElementArrayBuffer->getDeleted());
    134         fElementArrayBuffer->ref();
    135 
    136         GrAlwaysAssert(!fElementArrayBuffer->getBound());
    137         fElementArrayBuffer->setBound();
    138     }
    139 }
    140 
    141 void GrDebugGL::setTexture(GrTextureObj *texture)  {
    142     fTextureUnits[fCurTextureUnit]->setTexture(texture);
    143 }
    144 
    145 void GrDebugGL::setFrameBuffer(GrFrameBufferObj *frameBuffer)  {
    146     if (fFrameBuffer) {
    147         GrAlwaysAssert(fFrameBuffer->getBound());
    148         fFrameBuffer->resetBound();
    149 
    150         GrAlwaysAssert(!fFrameBuffer->getDeleted());
    151         fFrameBuffer->unref();
    152     }
    153 
    154     fFrameBuffer = frameBuffer;
    155 
    156     if (fFrameBuffer) {
    157         GrAlwaysAssert(!fFrameBuffer->getDeleted());
    158         fFrameBuffer->ref();
    159 
    160         GrAlwaysAssert(!fFrameBuffer->getBound());
    161         fFrameBuffer->setBound();
    162     }
    163 }
    164 
    165 void GrDebugGL::setRenderBuffer(GrRenderBufferObj *renderBuffer)  {
    166     if (fRenderBuffer) {
    167         GrAlwaysAssert(fRenderBuffer->getBound());
    168         fRenderBuffer->resetBound();
    169 
    170         GrAlwaysAssert(!fRenderBuffer->getDeleted());
    171         fRenderBuffer->unref();
    172     }
    173 
    174     fRenderBuffer = renderBuffer;
    175 
    176     if (fRenderBuffer) {
    177         GrAlwaysAssert(!fRenderBuffer->getDeleted());
    178         fRenderBuffer->ref();
    179 
    180         GrAlwaysAssert(!fRenderBuffer->getBound());
    181         fRenderBuffer->setBound();
    182     }
    183 }
    184 
    185 void GrDebugGL::useProgram(GrProgramObj *program) {
    186     if (fProgram) {
    187         GrAlwaysAssert(fProgram->getInUse());
    188         fProgram->resetInUse();
    189 
    190         GrAlwaysAssert(!fProgram->getDeleted());
    191         fProgram->unref();
    192     }
    193 
    194     fProgram = program;
    195 
    196     if (fProgram) {
    197         GrAlwaysAssert(!fProgram->getDeleted());
    198         fProgram->ref();
    199 
    200         GrAlwaysAssert(!fProgram->getInUse());
    201         fProgram->setInUse();
    202     }
    203 }
    204 
    205 void GrDebugGL::report() const {
    206     for (int i = 0; i < fObjects.count(); ++i) {
    207         GrAlwaysAssert(0 == fObjects[i]->getRefCount());
    208         GrAlwaysAssert(0 < fObjects[i]->getHighRefCount());
    209         GrAlwaysAssert(fObjects[i]->getDeleted());
    210     }
    211 }
    212