EGLLogWrapper.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1package android.opengl;
2
3import java.io.IOException;
4import java.io.Writer;
5
6import javax.microedition.khronos.egl.EGL;
7import javax.microedition.khronos.egl.EGL10;
8import javax.microedition.khronos.egl.EGL11;
9import javax.microedition.khronos.egl.EGLConfig;
10import javax.microedition.khronos.egl.EGLContext;
11import javax.microedition.khronos.egl.EGLDisplay;
12import javax.microedition.khronos.egl.EGLSurface;
13
14class EGLLogWrapper implements EGL11 {
15    private EGL10 mEgl10;
16    Writer mLog;
17    boolean mLogArgumentNames;
18    boolean mCheckError;
19    private int mArgCount;
20
21
22    public EGLLogWrapper(EGL egl, int configFlags, Writer log) {
23        mEgl10 = (EGL10) egl;
24        mLog = log;
25        mLogArgumentNames =
26            (GLDebugHelper.CONFIG_LOG_ARGUMENT_NAMES & configFlags) != 0;
27        mCheckError =
28            (GLDebugHelper.CONFIG_CHECK_GL_ERROR & configFlags) != 0;
29    }
30
31    public boolean eglChooseConfig(EGLDisplay display, int[] attrib_list,
32            EGLConfig[] configs, int config_size, int[] num_config) {
33        begin("eglChooseConfig");
34        arg("display", display);
35        arg("attrib_list", attrib_list);
36        arg("config_size", config_size);
37        end();
38
39        boolean result = mEgl10.eglChooseConfig(display, attrib_list, configs,
40                config_size, num_config);
41        arg("configs", configs);
42        arg("num_config", num_config);
43        returns(result);
44        checkError();
45        return result;
46    }
47
48    public boolean eglCopyBuffers(EGLDisplay display, EGLSurface surface,
49            Object native_pixmap) {
50        begin("eglCopyBuffers");
51        arg("display", display);
52        arg("surface", surface);
53        arg("native_pixmap", native_pixmap);
54        end();
55
56        boolean result = mEgl10.eglCopyBuffers(display, surface, native_pixmap);
57        returns(result);
58        checkError();
59        return result;
60    }
61
62    public EGLContext eglCreateContext(EGLDisplay display, EGLConfig config,
63            EGLContext share_context, int[] attrib_list) {
64        begin("eglCreateContext");
65        arg("display", display);
66        arg("config", config);
67        arg("share_context", share_context);
68        arg("attrib_list", attrib_list);
69        end();
70
71        EGLContext result = mEgl10.eglCreateContext(display, config,
72                share_context, attrib_list);
73        returns(result);
74        checkError();
75        return result;
76    }
77
78    public EGLSurface eglCreatePbufferSurface(EGLDisplay display,
79            EGLConfig config, int[] attrib_list) {
80        begin("eglCreatePbufferSurface");
81        arg("display", display);
82        arg("config", config);
83        arg("attrib_list", attrib_list);
84        end();
85
86        EGLSurface result = mEgl10.eglCreatePbufferSurface(display, config,
87                attrib_list);
88        returns(result);
89        checkError();
90        return result;
91    }
92
93    public EGLSurface eglCreatePixmapSurface(EGLDisplay display,
94            EGLConfig config, Object native_pixmap, int[] attrib_list) {
95        begin("eglCreatePixmapSurface");
96        arg("display", display);
97        arg("config", config);
98        arg("native_pixmap", native_pixmap);
99        arg("attrib_list", attrib_list);
100        end();
101
102        EGLSurface result = mEgl10.eglCreatePixmapSurface(display, config,
103                native_pixmap, attrib_list);
104        returns(result);
105        checkError();
106        return result;
107        }
108
109    public EGLSurface eglCreateWindowSurface(EGLDisplay display,
110            EGLConfig config, Object native_window, int[] attrib_list) {
111        begin("eglCreateWindowSurface");
112        arg("display", display);
113        arg("config", config);
114        arg("native_window", native_window);
115        arg("attrib_list", attrib_list);
116        end();
117
118        EGLSurface result = mEgl10.eglCreateWindowSurface(display, config,
119                native_window, attrib_list);
120        returns(result);
121        checkError();
122        return result;
123    }
124
125    public boolean eglDestroyContext(EGLDisplay display, EGLContext context) {
126        begin("eglDestroyContext");
127        arg("display", display);
128        arg("context", context);
129        end();
130
131        boolean result = mEgl10.eglDestroyContext(display, context);
132        returns(result);
133        checkError();
134        return result;
135    }
136
137    public boolean eglDestroySurface(EGLDisplay display, EGLSurface surface) {
138        begin("eglDestroySurface");
139        arg("display", display);
140        arg("surface", surface);
141        end();
142
143        boolean result = mEgl10.eglDestroySurface(display, surface);
144        returns(result);
145        checkError();
146        return result;
147    }
148
149    public boolean eglGetConfigAttrib(EGLDisplay display, EGLConfig config,
150            int attribute, int[] value) {
151        begin("eglGetConfigAttrib");
152        arg("display", display);
153        arg("config", config);
154        arg("attribute", attribute);
155        end();
156        boolean result = mEgl10.eglGetConfigAttrib(display, config, attribute,
157                value);
158        arg("value", value);
159        returns(result);
160        checkError();
161        return false;
162    }
163
164    public boolean eglGetConfigs(EGLDisplay display, EGLConfig[] configs,
165            int config_size, int[] num_config) {
166        begin("eglGetConfigs");
167        arg("display", display);
168        arg("config_size", config_size);
169        end();
170
171        boolean result = mEgl10.eglGetConfigs(display, configs, config_size,
172                num_config);
173        arg("configs", configs);
174        arg("num_config", num_config);
175        returns(result);
176        checkError();
177        return result;
178    }
179
180    public EGLContext eglGetCurrentContext() {
181        begin("eglGetCurrentContext");
182        end();
183
184        EGLContext result = mEgl10.eglGetCurrentContext();
185        returns(result);
186
187        checkError();
188        return result;
189    }
190
191    public EGLDisplay eglGetCurrentDisplay() {
192        begin("eglGetCurrentDisplay");
193        end();
194
195        EGLDisplay result = mEgl10.eglGetCurrentDisplay();
196        returns(result);
197
198        checkError();
199        return result;
200    }
201
202    public EGLSurface eglGetCurrentSurface(int readdraw) {
203        begin("eglGetCurrentSurface");
204        arg("readdraw", readdraw);
205        end();
206
207        EGLSurface result = mEgl10.eglGetCurrentSurface(readdraw);
208        returns(result);
209
210        checkError();
211        return result;
212    }
213
214    public EGLDisplay eglGetDisplay(Object native_display) {
215        begin("eglGetDisplay");
216        arg("native_display", native_display);
217        end();
218
219        EGLDisplay result = mEgl10.eglGetDisplay(native_display);
220        returns(result);
221
222        checkError();
223        return result;
224    }
225
226    public int eglGetError() {
227        begin("eglGetError");
228        end();
229
230        int result = mEgl10.eglGetError();
231        returns(getErrorString(result));
232
233        return result;
234    }
235
236    public boolean eglInitialize(EGLDisplay display, int[] major_minor) {
237        begin("eglInitialize");
238        arg("display", display);
239        end();
240        boolean result = mEgl10.eglInitialize(display, major_minor);
241        returns(result);
242        arg("major_minor", major_minor);
243        checkError();
244        return result;
245    }
246
247    public boolean eglMakeCurrent(EGLDisplay display, EGLSurface draw,
248            EGLSurface read, EGLContext context) {
249        begin("eglMakeCurrent");
250        arg("display", display);
251        arg("draw", draw);
252        arg("read", read);
253        arg("context", context);
254        end();
255        boolean result = mEgl10.eglMakeCurrent(display, draw, read, context);
256        returns(result);
257        checkError();
258        return result;
259    }
260
261    public boolean eglQueryContext(EGLDisplay display, EGLContext context,
262            int attribute, int[] value) {
263        begin("eglQueryContext");
264        arg("display", display);
265        arg("context", context);
266        arg("attribute", attribute);
267        end();
268        boolean result = mEgl10.eglQueryContext(display, context, attribute,
269                value);
270        returns(value[0]);
271        returns(result);
272        checkError();
273        return result;
274    }
275
276    public String eglQueryString(EGLDisplay display, int name) {
277        begin("eglQueryString");
278        arg("display", display);
279        arg("name", name);
280        end();
281        String result = mEgl10.eglQueryString(display, name);
282        returns(result);
283        checkError();
284        return result;
285    }
286
287    public boolean eglQuerySurface(EGLDisplay display, EGLSurface surface,
288            int attribute, int[] value) {
289        begin("eglQuerySurface");
290        arg("display", display);
291        arg("surface", surface);
292        arg("attribute", attribute);
293        end();
294        boolean result = mEgl10.eglQuerySurface(display, surface, attribute,
295                value);
296        returns(value[0]);
297        returns(result);
298        checkError();
299        return result;
300    }
301
302    public boolean eglSwapBuffers(EGLDisplay display, EGLSurface surface) {
303        begin("eglInitialize");
304        arg("display", display);
305        arg("surface", surface);
306        end();
307        boolean result = mEgl10.eglSwapBuffers(display, surface);
308        returns(result);
309        checkError();
310        return result;
311    }
312
313    public boolean eglTerminate(EGLDisplay display) {
314        begin("eglTerminate");
315        arg("display", display);
316        end();
317        boolean result = mEgl10.eglTerminate(display);
318        returns(result);
319        checkError();
320        return result;
321    }
322
323    public boolean eglWaitGL() {
324        begin("eglWaitGL");
325        end();
326        boolean result = mEgl10.eglWaitGL();
327        returns(result);
328        checkError();
329        return result;
330    }
331
332    public boolean eglWaitNative(int engine, Object bindTarget) {
333        begin("eglWaitNative");
334        arg("engine", engine);
335        arg("bindTarget", bindTarget);
336        end();
337        boolean result = mEgl10.eglWaitNative(engine, bindTarget);
338        returns(result);
339        checkError();
340        return result;
341    }
342
343    private void checkError() {
344        int eglError;
345        if ((eglError = mEgl10.eglGetError()) != EGL_SUCCESS) {
346            String errorMessage = "eglError: " + getErrorString(eglError);
347            logLine(errorMessage);
348            if (mCheckError) {
349                throw new GLException(eglError, errorMessage);
350            }
351        }
352    }
353
354    private void logLine(String message) {
355        log(message + '\n');
356    }
357
358    private void log(String message) {
359        try {
360            mLog.write(message);
361        } catch (IOException e) {
362            // Ignore exception, keep on trying
363        }
364    }
365
366    private void begin(String name) {
367        log(name + '(');
368        mArgCount = 0;
369    }
370
371    private void arg(String name, String value) {
372        if (mArgCount++ > 0) {
373            log(", ");
374        }
375        if (mLogArgumentNames) {
376            log(name + "=");
377        }
378        log(value);
379    }
380
381    private void end() {
382        log(");\n");
383        flush();
384    }
385
386    private void flush() {
387        try {
388            mLog.flush();
389        } catch (IOException e) {
390            mLog = null;
391        }
392    }
393
394    private void arg(String name, int value) {
395        arg(name, Integer.toString(value));
396    }
397
398    private void arg(String name, Object object) {
399        arg(name, toString(object));
400    }
401
402    private void arg(String name, EGLDisplay object) {
403        if (object == EGL10.EGL_DEFAULT_DISPLAY) {
404            arg(name, "EGL10.EGL_DEFAULT_DISPLAY");
405        } else if (object == EGL_NO_DISPLAY) {
406            arg(name, "EGL10.EGL_NO_DISPLAY");
407        } else {
408            arg(name, toString(object));
409        }
410    }
411
412    private void arg(String name, EGLContext object) {
413        if (object == EGL10.EGL_NO_CONTEXT) {
414            arg(name, "EGL10.EGL_NO_CONTEXT");
415        } else {
416            arg(name, toString(object));
417        }
418    }
419
420    private void arg(String name, EGLSurface object) {
421        if (object == EGL10.EGL_NO_SURFACE) {
422            arg(name, "EGL10.EGL_NO_SURFACE");
423        } else {
424            arg(name, toString(object));
425        }
426    }
427
428    private void returns(String result) {
429        log(" returns " + result + ";\n");
430        flush();
431    }
432
433    private void returns(int result) {
434        returns(Integer.toString(result));
435    }
436
437    private void returns(boolean result) {
438        returns(Boolean.toString(result));
439    }
440
441    private void returns(Object result) {
442        returns(toString(result));
443    }
444
445    private String toString(Object obj) {
446        if (obj == null) {
447            return "null";
448        } else {
449            return obj.toString();
450        }
451    }
452
453    private void arg(String name, int[] arr) {
454        if (arr == null) {
455            arg(name, "null");
456        } else {
457            arg(name, toString(arr.length, arr, 0));
458        }
459    }
460
461    private void arg(String name, Object[] arr) {
462        if (arr == null) {
463            arg(name, "null");
464        } else {
465            arg(name, toString(arr.length, arr, 0));
466        }
467    }
468
469    private String toString(int n, int[] arr, int offset) {
470        StringBuilder buf = new StringBuilder();
471        buf.append("{\n");
472        int arrLen = arr.length;
473        for (int i = 0; i < n; i++) {
474            int index = offset + i;
475            buf.append(" [" + index + "] = ");
476            if (index < 0 || index >= arrLen) {
477                buf.append("out of bounds");
478            } else {
479                buf.append(arr[index]);
480            }
481            buf.append('\n');
482        }
483        buf.append("}");
484        return buf.toString();
485    }
486
487    private String toString(int n, Object[] arr, int offset) {
488        StringBuilder buf = new StringBuilder();
489        buf.append("{\n");
490        int arrLen = arr.length;
491        for (int i = 0; i < n; i++) {
492            int index = offset + i;
493            buf.append(" [" + index + "] = ");
494            if (index < 0 || index >= arrLen) {
495                buf.append("out of bounds");
496            } else {
497                buf.append(arr[index]);
498            }
499            buf.append('\n');
500        }
501        buf.append("}");
502        return buf.toString();
503    }
504
505    private static String getHex(int value) {
506        return "0x" + Integer.toHexString(value);
507    }
508
509    public static String getErrorString(int error) {
510        switch (error) {
511        case EGL_SUCCESS:
512            return "EGL_SUCCESS";
513        case EGL_NOT_INITIALIZED:
514            return "EGL_NOT_INITIALIZED";
515        case EGL_BAD_ACCESS:
516            return "EGL_BAD_ACCESS";
517        case EGL_BAD_ALLOC:
518            return "EGL_BAD_ALLOC";
519        case EGL_BAD_ATTRIBUTE:
520            return "EGL_BAD_ATTRIBUTE";
521        case EGL_BAD_CONFIG:
522            return "EGL_BAD_CONFIG";
523        case EGL_BAD_CONTEXT:
524            return "EGL_BAD_CONTEXT";
525        case EGL_BAD_CURRENT_SURFACE:
526            return "EGL_BAD_CURRENT_SURFACE";
527        case EGL_BAD_DISPLAY:
528            return "EGL_BAD_DISPLAY";
529        case EGL_BAD_MATCH:
530            return "EGL_BAD_MATCH";
531        case EGL_BAD_NATIVE_PIXMAP:
532            return "EGL_BAD_NATIVE_PIXMAP";
533        case EGL_BAD_NATIVE_WINDOW:
534            return "EGL_BAD_NATIVE_WINDOW";
535        case EGL_BAD_PARAMETER:
536            return "EGL_BAD_PARAMETER";
537        case EGL_BAD_SURFACE:
538            return "EGL_BAD_SURFACE";
539        case EGL11.EGL_CONTEXT_LOST:
540            return "EGL_CONTEXT_LOST";
541        default:
542            return getHex(error);
543        }
544    }
545}
546