Skip to content

Commit 2379cfb

Browse files
authored
Merge pull request jMonkeyEngine#1276 from MeFisto94/fix-gldebug
Fixes jMonkeyEngine#1272 - Improve GLDebugDesktop by reducing manual overrides
2 parents c198b1d + df39677 commit 2379cfb

File tree

9 files changed

+122
-962
lines changed

9 files changed

+122
-962
lines changed

jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,7 @@
5252
import com.jme3.input.dummy.DummyKeyInput;
5353
import com.jme3.input.dummy.DummyMouseInput;
5454
import com.jme3.renderer.android.AndroidGL;
55-
import com.jme3.renderer.opengl.GL;
56-
import com.jme3.renderer.opengl.GLES_30;
57-
import com.jme3.renderer.opengl.GLDebugES;
58-
import com.jme3.renderer.opengl.GLExt;
59-
import com.jme3.renderer.opengl.GLFbo;
60-
import com.jme3.renderer.opengl.GLRenderer;
61-
import com.jme3.renderer.opengl.GLTracer;
55+
import com.jme3.renderer.opengl.*;
6256
import com.jme3.system.*;
6357
import com.jme3.util.AndroidBufferAllocator;
6458
import com.jme3.util.BufferAllocatorFactory;
@@ -209,14 +203,14 @@ public void uncaughtException(Thread thread, Throwable thrown) {
209203
});
210204

211205
timer = new NanoTimer();
212-
Object gl = new AndroidGL();
206+
GL gl = new AndroidGL();
213207
if (settings.getBoolean("GraphicsDebug")) {
214-
gl = new GLDebugES((GL) gl, (GLExt) gl, (GLFbo) gl);
208+
gl = (GL) GLDebug.createProxy(gl, gl, GL.class, GL2.class, GLES_30.class, GLFbo.class, GLExt.class);
215209
}
216210
if (settings.getBoolean("GraphicsTrace")) {
217-
gl = GLTracer.createGlesTracer(gl, GL.class, GLES_30.class, GLFbo.class, GLExt.class);
211+
gl = (GL)GLTracer.createGlesTracer(gl, GL.class, GLES_30.class, GLFbo.class, GLExt.class);
218212
}
219-
renderer = new GLRenderer((GL)gl, (GLExt)gl, (GLFbo)gl);
213+
renderer = new GLRenderer(gl, (GLExt)gl, (GLFbo)gl);
220214
renderer.initialize();
221215

222216
JmeSystem.setSoftTextDialogInput(this);

jme3-core/src/main/java/com/jme3/renderer/opengl/GLDebug.java

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,66 @@
1+
/*
2+
* Copyright (c) 2009-2020 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
132
package com.jme3.renderer.opengl;
233

334
import com.jme3.renderer.RendererException;
435

5-
public abstract class GLDebug {
36+
import java.lang.reflect.InvocationHandler;
37+
import java.lang.reflect.Method;
38+
import java.lang.reflect.Proxy;
639

40+
/**
41+
* This class uses Reflection to intercept method calls to the Proxy Object ({@link #createProxy(GL, Object, Class[])}
42+
* and extends them with the Error Checking in {@link #checkError()}.<br/>
43+
* This means we don't have to generate a class with overrides for every possible method just to add a
44+
* {@link #checkError()} call.<br/>
45+
* Note that we should not call {@link #checkError()} for {@link GL#glGetError()}, it doesn't make sense.<br />
46+
* Note that this class is general purpose and as such every class instance (every object) can be guarded as long as
47+
* the passed gl instance is valid.<br/>
48+
*
49+
* @author MeFisto94
50+
*/
51+
public class GLDebug implements InvocationHandler {
52+
protected Object obj;
753
protected GL gl;
8-
54+
protected Method methodGlGetError;
55+
56+
private GLDebug(GL gl, Object obj) throws NoSuchMethodException {
57+
this.gl = gl;
58+
this.obj = obj;
59+
methodGlGetError = GL.class.getMethod("glGetError");
60+
/* The NoSuchMethodException shouldn't be thrown, but since we're in a constructor and cannot fail safe
61+
* otherwise, we throw it. */
62+
}
63+
964
protected String decodeError(int err) {
1065
String errMsg;
1166
switch (err) {
@@ -46,4 +101,38 @@ protected void checkError() {
46101
throw new RendererException("An OpenGL error occurred - " + decodeError(err));
47102
}
48103
}
104+
105+
@Override
106+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
107+
Object result = method.invoke(obj, args);
108+
109+
if (method.equals(methodGlGetError)) {
110+
return result;
111+
}
112+
113+
checkError();
114+
return result;
115+
}
116+
117+
/**
118+
* Creates a debug-proxied object, which will call {@link GL#glGetError()} after every method invocation and throw
119+
* a {@link com.jme3.renderer.RendererException} if there was a GL Error.
120+
*
121+
* @param gl The GL Context, required to call {@link GL#glGetError()}
122+
* @param obj The object which methods will be proxied
123+
* @param implementedInterfaces The interfaces/class this object implements
124+
* @return The Proxy object (or null if an error occured)
125+
*/
126+
public static Object createProxy(GL gl, Object obj, Class<?>... implementedInterfaces) {
127+
try {
128+
return Proxy.newProxyInstance(
129+
GLDebug.class.getClassLoader(),
130+
implementedInterfaces,
131+
new GLDebug(gl, obj)
132+
);
133+
} catch (NoSuchMethodException nsme) {
134+
throw new IllegalArgumentException ("Could not initialize the proxy because the glGetError method wasn't " +
135+
"found!", nsme);
136+
}
137+
}
49138
}

jme3-core/src/main/java/com/jme3/renderer/opengl/GLDebugDesktop.java

Lines changed: 0 additions & 147 deletions
This file was deleted.

0 commit comments

Comments
 (0)