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