1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.opengl;
18 
19 import java.io.Writer;
20 
21 import javax.microedition.khronos.egl.EGL;
22 import javax.microedition.khronos.opengles.GL;
23 
24 /**
25  * A helper class for debugging OpenGL ES applications.
26  *
27  * Wraps the supplied GL interface with a new GL interface that adds support for
28  * error checking and logging.
29  *
30  */
31 public class GLDebugHelper {
32 
33     /**
34      * Wrap an existing GL interface in a new GL interface that adds support for
35      * error checking and/or logging.
36      * <p>
37      * Wrapping means that the GL instance that is passed in to this method is
38      * wrapped inside a new GL instance that optionally performs additional
39      * operations before and after calling the wrapped GL instance.
40      * <p>
41      * Error checking means that the wrapper will automatically call
42      * glError after each GL operation,
43      * and throw a GLException if an error occurs. (By design, calling glError
44      * itself will not cause an exception to be thrown.) Enabling error checking
45      * is an alternative to manually calling glError after every GL operation.
46      * <p>
47      * Logging means writing a text representation of each GL method call to
48      * a log.
49      * <p>
50      * @param gl the existing GL interface. Must implement GL and GL10. May
51      * optionally implement GL11 as well.
52      * @param configFlags A bitmask of error checking flags.
53      * @param log - null to disable logging, non-null to enable logging.
54      * @return the wrapped GL instance.
55      */
56 
57     /**
58      * Check glError() after every call.
59      */
60     public static final int CONFIG_CHECK_GL_ERROR = (1 << 0);
61 
62     /**
63      * Check if all calls are on the same thread.
64      */
65     public static final int CONFIG_CHECK_THREAD = (1 << 1);
66 
67     /**
68      * Print argument names when logging GL Calls.
69      */
70     public static final int CONFIG_LOG_ARGUMENT_NAMES = (1 << 2);
71 
72     /**
73      * The Error number used in the GLException that is thrown if
74      * CONFIG_CHECK_THREAD is enabled and you call OpenGL ES on the
75      * a different thread.
76      */
77     public static final int ERROR_WRONG_THREAD = 0x7000;
78 
wrap(GL gl, int configFlags, Writer log)79     public static GL wrap(GL gl, int configFlags, Writer log) {
80         if ( configFlags != 0 ) {
81             gl = new GLErrorWrapper(gl, configFlags);
82         }
83         if ( log != null ) {
84             boolean logArgumentNames =
85                 (CONFIG_LOG_ARGUMENT_NAMES & configFlags) != 0;
86             gl = new GLLogWrapper(gl, log, logArgumentNames);
87         }
88         return gl;
89     }
90 
91     /**
92      * Wrap an existing EGL interface in a new EGL interface that adds
93      * support for error checking and/or logging.
94      * @param egl the existing GL interface. Must implement EGL and EGL10. May
95      * optionally implement EGL11 as well.
96      * @param configFlags A bitmask of error checking flags.
97      * @param log - null to disable logging, non-null to enable logging.
98      * @return the wrapped EGL interface.
99      */
wrap(EGL egl, int configFlags, Writer log)100     public static EGL wrap(EGL egl, int configFlags, Writer log) {
101         if (log != null) {
102             egl = new EGLLogWrapper(egl, configFlags, log);
103         }
104         return egl;
105     }
106 }
107 
108