1603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
2603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com/*
3603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com * Copyright 2012 Google Inc.
4603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com *
5603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com * Use of this source code is governed by a BSD-style license that can be
6603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com * found in the LICENSE file.
7603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com */
8603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
9603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com#include "GrDebugGL.h"
10603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com#include "GrTextureObj.h"
11603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com#include "GrBufferObj.h"
12603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com#include "GrRenderBufferObj.h"
13603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com#include "GrFrameBufferObj.h"
14603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com#include "GrShaderObj.h"
15603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com#include "GrProgramObj.h"
16603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com#include "GrTextureUnitObj.h"
176ccccc065b1122fec12480699d10e7e7482a8b76bsalomon@google.com#include "GrVertexArrayObj.h"
18603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
19ab8e565ff2aa849901f924f9fba827c80e093e7crobertphillips@google.comGrDebugGL* GrDebugGL::gObj = NULL;
20ab8e565ff2aa849901f924f9fba827c80e093e7crobertphillips@google.comint GrDebugGL::gStaticRefCount = 0;
21603687589970f5984b4c2fd42e76f443215137barobertphillips@google.comGrDebugGL::Create GrDebugGL::gFactoryFunc[kObjTypeCount] = {
22603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    GrTextureObj::createGrTextureObj,
23603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    GrBufferObj::createGrBufferObj,
24603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    GrRenderBufferObj::createGrRenderBufferObj,
25603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    GrFrameBufferObj::createGrFrameBufferObj,
26603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    GrShaderObj::createGrShaderObj,
27603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    GrProgramObj::createGrProgramObj,
28603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    GrTextureUnitObj::createGrTextureUnitObj,
296ccccc065b1122fec12480699d10e7e7482a8b76bsalomon@google.com    GrVertexArrayObj::createGrVertexArrayObj,
30603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com};
31603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
32603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
33935e9f4fafdfc64130e6be9ea2bb30e3bafd852armistry@google.comGrDebugGL::GrDebugGL()
3472f0a2948467d35053a9af6c28fea9110dcd65d0robertphillips@google.com    : fPackRowLength(0)
3572f0a2948467d35053a9af6c28fea9110dcd65d0robertphillips@google.com    , fUnPackRowLength(0)
3672f0a2948467d35053a9af6c28fea9110dcd65d0robertphillips@google.com    , fCurTextureUnit(0)
37603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    , fArrayBuffer(NULL)
38603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    , fElementArrayBuffer(NULL)
39603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    , fFrameBuffer(NULL)
40603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    , fRenderBuffer(NULL)
41603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    , fProgram(NULL)
426ccccc065b1122fec12480699d10e7e7482a8b76bsalomon@google.com    , fTexture(NULL)
436ccccc065b1122fec12480699d10e7e7482a8b76bsalomon@google.com    , fVertexArray(NULL)  {
44603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
45603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    for (int i = 0; i < kDefaultMaxTextureUnits; ++i) {
46ab8e565ff2aa849901f924f9fba827c80e093e7crobertphillips@google.com
47ab8e565ff2aa849901f924f9fba827c80e093e7crobertphillips@google.com        fTextureUnits[i] = reinterpret_cast<GrTextureUnitObj *>(
48ab8e565ff2aa849901f924f9fba827c80e093e7crobertphillips@google.com                            createObj(GrDebugGL::kTextureUnit_ObjTypes));
49603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fTextureUnits[i]->ref();
50603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
51603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fTextureUnits[i]->setNumber(i);
52603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
53603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com}
54603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
55603687589970f5984b4c2fd42e76f443215137barobertphillips@google.comGrDebugGL::~GrDebugGL() {
56603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    // unref & delete the texture units first so they don't show up on the leak report
57603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    for (int i = 0; i < kDefaultMaxTextureUnits; ++i) {
58603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fTextureUnits[i]->unref();
59603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fTextureUnits[i]->deleteAction();
60603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
61603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
62603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    this->report();
63603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
64603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    for (int i = 0; i < fObjects.count(); ++i) {
65603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        delete fObjects[i];
66603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
67603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    fObjects.reset();
68603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
69603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    fArrayBuffer = NULL;
70603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    fElementArrayBuffer = NULL;
71603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    fFrameBuffer = NULL;
72603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    fRenderBuffer = NULL;
73603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    fProgram = NULL;
74603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    fTexture = NULL;
756ccccc065b1122fec12480699d10e7e7482a8b76bsalomon@google.com    fVertexArray = NULL;
76603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com}
77603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
78603687589970f5984b4c2fd42e76f443215137barobertphillips@google.comGrFakeRefObj *GrDebugGL::findObject(GrGLuint ID, GrObjTypes type) {
79603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    for (int i = 0; i < fObjects.count(); ++i) {
80603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        if (fObjects[i]->getID() == ID) { // && fObjects[i]->getType() == type) {
81603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com            // The application shouldn't be accessing objects
82935e9f4fafdfc64130e6be9ea2bb30e3bafd852armistry@google.com            // that (as far as OpenGL knows) were already deleted
83603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com            GrAlwaysAssert(!fObjects[i]->getDeleted());
84603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com            GrAlwaysAssert(!fObjects[i]->getMarkedForDeletion());
85603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com            return fObjects[i];
86603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        }
87603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
88603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
89603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    return NULL;
90603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com}
91603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
92935e9f4fafdfc64130e6be9ea2bb30e3bafd852armistry@google.comvoid GrDebugGL::setArrayBuffer(GrBufferObj *arrayBuffer) {
93603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    if (fArrayBuffer) {
94603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        // automatically break the binding of the old buffer
95603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(fArrayBuffer->getBound());
96603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fArrayBuffer->resetBound();
97603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
98603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fArrayBuffer->getDeleted());
99603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fArrayBuffer->unref();
100603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
101603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
102935e9f4fafdfc64130e6be9ea2bb30e3bafd852armistry@google.com    fArrayBuffer = arrayBuffer;
103603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
104603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    if (fArrayBuffer) {
105603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fArrayBuffer->getDeleted());
106603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fArrayBuffer->ref();
107603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
108603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fArrayBuffer->getBound());
109603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fArrayBuffer->setBound();
110603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
111603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com}
112603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
1136ccccc065b1122fec12480699d10e7e7482a8b76bsalomon@google.comvoid GrDebugGL::setVertexArray(GrVertexArrayObj* vertexArray) {
1146ccccc065b1122fec12480699d10e7e7482a8b76bsalomon@google.com    if (NULL != vertexArray) {
115a91959e0e4997d3a73b38e6d7f0296c566a4b1d0tfarina@chromium.org        SkASSERT(!vertexArray->getDeleted());
1166ccccc065b1122fec12480699d10e7e7482a8b76bsalomon@google.com    }
1176ccccc065b1122fec12480699d10e7e7482a8b76bsalomon@google.com    SkRefCnt_SafeAssign(fVertexArray, vertexArray);
1186ccccc065b1122fec12480699d10e7e7482a8b76bsalomon@google.com}
1196ccccc065b1122fec12480699d10e7e7482a8b76bsalomon@google.com
120935e9f4fafdfc64130e6be9ea2bb30e3bafd852armistry@google.comvoid GrDebugGL::setElementArrayBuffer(GrBufferObj *elementArrayBuffer) {
121603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    if (fElementArrayBuffer) {
122603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        // automatically break the binding of the old buffer
123603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(fElementArrayBuffer->getBound());
124603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fElementArrayBuffer->resetBound();
125603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
126603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fElementArrayBuffer->getDeleted());
127603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fElementArrayBuffer->unref();
128603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
129603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
130935e9f4fafdfc64130e6be9ea2bb30e3bafd852armistry@google.com    fElementArrayBuffer = elementArrayBuffer;
131603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
132603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    if (fElementArrayBuffer) {
133603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fElementArrayBuffer->getDeleted());
134603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fElementArrayBuffer->ref();
135603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
136603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fElementArrayBuffer->getBound());
137603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fElementArrayBuffer->setBound();
138603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
139603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com}
140603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
141935e9f4fafdfc64130e6be9ea2bb30e3bafd852armistry@google.comvoid GrDebugGL::setTexture(GrTextureObj *texture)  {
142603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    fTextureUnits[fCurTextureUnit]->setTexture(texture);
143603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com}
144603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
145935e9f4fafdfc64130e6be9ea2bb30e3bafd852armistry@google.comvoid GrDebugGL::setFrameBuffer(GrFrameBufferObj *frameBuffer)  {
146603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    if (fFrameBuffer) {
147603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(fFrameBuffer->getBound());
148603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fFrameBuffer->resetBound();
149603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
150603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fFrameBuffer->getDeleted());
151603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fFrameBuffer->unref();
152603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
153603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
154935e9f4fafdfc64130e6be9ea2bb30e3bafd852armistry@google.com    fFrameBuffer = frameBuffer;
155603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
156603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    if (fFrameBuffer) {
157603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fFrameBuffer->getDeleted());
158603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fFrameBuffer->ref();
159603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
160603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fFrameBuffer->getBound());
161603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fFrameBuffer->setBound();
162603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
163603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com}
164603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
165935e9f4fafdfc64130e6be9ea2bb30e3bafd852armistry@google.comvoid GrDebugGL::setRenderBuffer(GrRenderBufferObj *renderBuffer)  {
166603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    if (fRenderBuffer) {
167603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(fRenderBuffer->getBound());
168603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fRenderBuffer->resetBound();
169603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
170603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fRenderBuffer->getDeleted());
171603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fRenderBuffer->unref();
172603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
173603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
174935e9f4fafdfc64130e6be9ea2bb30e3bafd852armistry@google.com    fRenderBuffer = renderBuffer;
175603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
176603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    if (fRenderBuffer) {
177603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fRenderBuffer->getDeleted());
178603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fRenderBuffer->ref();
179603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
180603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fRenderBuffer->getBound());
181603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fRenderBuffer->setBound();
182603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
183603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com}
184603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
185603687589970f5984b4c2fd42e76f443215137barobertphillips@google.comvoid GrDebugGL::useProgram(GrProgramObj *program) {
186603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    if (fProgram) {
187603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(fProgram->getInUse());
188603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fProgram->resetInUse();
189603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
190603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fProgram->getDeleted());
191603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fProgram->unref();
192603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
193603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
194603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    fProgram = program;
195603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
196603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    if (fProgram) {
197603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fProgram->getDeleted());
198603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fProgram->ref();
199603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
200603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(!fProgram->getInUse());
201603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        fProgram->setInUse();
202603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
203603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com}
204603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com
205603687589970f5984b4c2fd42e76f443215137barobertphillips@google.comvoid GrDebugGL::report() const {
206603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    for (int i = 0; i < fObjects.count(); ++i) {
207603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(0 == fObjects[i]->getRefCount());
208603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(0 < fObjects[i]->getHighRefCount());
209603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com        GrAlwaysAssert(fObjects[i]->getDeleted());
210603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com    }
211603687589970f5984b4c2fd42e76f443215137barobertphillips@google.com}
212