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